LineItem->setPos(0,returnLinePosition());
LineItem->setPos(0,0);
int n=100; LineItem->setPos(0,n);
#include "mainwindow.h"#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this); //setWindowFlags(Qt::FramelessWindowHint); Scene=new QGraphicsScene(this); View=new QGraphicsView(Scene); setGeometry(300,100,400,600); View->setParent(this); setMaximumSize(width(),height()); View->setFixedSize(width(),height()); View->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); View->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); LineItem=Scene->addRect(QRect(-(width()/2)+1,0,width()-2,0),QPen(Qt::blue,2)); H=height(); LinePosition=600; backGroundGradient=new QLinearGradient(QPoint(0,H),QPoint(0,0)); for(int i=0;i<3;i++) { FC[i]=0; SC[i]=0; LC[i]=0; } std::thread BackGroundTH{&MainWindow::on_BACKgroundChange,this}; BackGroundTH.detach(); std::thread MoveLineTH{&MainWindow::Line_Move,this}; MoveLineTH.detach();// timer=new QTimer(this);// connect(timer,SIGNAL(timeout()),this,SLOT(Line_Move()));// timer->start(100);}MainWindow::~MainWindow(){ delete ui;}void MainWindow::newPosition_line(){ linePositionMutex.lock(); LineItem->setPos(0,returnLinePosition()); linePositionMutex.unlock();}void MainWindow::Line_Move(){ //timer->stop(); int newPosition=0; int C=1; linePositionMutex.lock(); int n=1+qrand()%100; for(int i=0;i<n;i++) { newPosition+=qrand()%(H*2); } linePositionMutex.unlock(); newPosition/=n; if(LinePosition<newPosition) C=1; else C=-1; for(int i=0;i<fabs(LinePosition-newPosition);i++) { LinePosition+=C*1; linePositionMutex.lock(); QThread::msleep(10); linePositionMutex.unlock(); newPosition_line(); } //timer->start(100); Line_Move();}int MainWindow::returnLinePosition(){ return LinePosition-(H-1);}void MainWindow::on_backgroundColor_change(){ backGroundMutex.lock(); Scene->setBackgroundBrush(returnGradient()); backGroundMutex.unlock();}void MainWindow::on_BACKgroundChange(){ int masF[3]={0,0,0}; int masS[3]={0,0,0}; int C=1; srand(QTime::currentTime().toString("s").toInt()); backGroundMutex.lock(); for(int i=0;i<3;i++) { masF[i]=rand()%255; masS[i]=rand()%255; } backGroundMutex.unlock(); for(int i=0;i<3;i++) { if(FC[i]<masF[i]) C=1; else C=-1; for(int j=0;j<fabs(FC[i]-masF[i]);j++) { backGroundMutex.lock(); QThread::usleep(100000); backGroundMutex.unlock(); FC[i]+=C*1; backGroundGradient->setColorAt(0,QColor(SC[0],SC[1],SC[2])); backGroundGradient->setColorAt(0.9,QColor(FC[0],FC[1],FC[2])); on_backgroundColor_change(); } } for(int i=0;i<3;i++) { if(SC[i]<masS[i]) C=1; else C=-1; for(int j=0;j<fabs(SC[i]-masS[i]);j++) { backGroundMutex.lock(); QThread::usleep(100000); backGroundMutex.unlock(); SC[i]+=C*1; backGroundGradient->setColorAt(0,QColor(SC[0],SC[1],SC[2])); backGroundGradient->setColorAt(0.9,QColor(FC[0],FC[1],FC[2])); on_backgroundColor_change(); } } on_BACKgroundChange();}QGradient MainWindow::returnGradient(){ return *backGroundGradient;}
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>#include <QGraphicsScene>#include <QGraphicsView>#include <QGraphicsItem>#include <thread>#include <mutex>#include <QThread>#include <QTime>#include <QTimer>namespace Ui {class MainWindow;}class MainWindow : public QMainWindow{ Q_OBJECTpublic: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); void newPosition_line(); void Line_Move(); int returnLinePosition(); void on_backgroundColor_change(); void on_BACKgroundChange(); QGradient returnGradient();public slots: //void Line_Move();signals:private: Ui::MainWindow *ui; QGraphicsScene *Scene; QGraphicsView *View; QGraphicsItem *LineItem; int LinePosition; int H; int FC[3]; int SC[3]; int LC[3]; QGradient *backGroundGradient; std::mutex backGroundMutex; std::mutex linePositionMutex; //QTimer *timer;};#endif // MAINWINDOW_H
QObject::startTimer: Timers cannot be started from another thread
std::thread BackGroundTH{&MainWindow::on_BACKgroundChange,this};BackGroundTH.detach();std::thread MoveLineTH{&MainWindow::Line_Move,this};MoveLineTH.detach();