多线程"/>
Qt笔记(10) qt多线程
介绍了Qt5 中 moveToThread() 方式开启线程,
以画图为载体。同时测试了信号槽连接函数第5个参数的不同枚举情况。
#ifndef MYTHREAD_H
#define MYTHREAD_H#include <QThread>
#include <QImage>class MyThread : public QThread
{Q_OBJECT
public:explicit MyThread(QObject *parent = nullptr);signals:void finishImage(const QImage image);
public slots:void drawImage();void myt2slot();void myt3slot();void myt4slot();
};#endif // MYTHREAD_H
#ifndef MYWIDGET_H
#define MYWIDGET_H#include <QWidget>
#include "mythread.h"
#include <QImage>
#include <QThread>
namespace Ui {
class MyWidget;
}class MyWidget : public QWidget
{Q_OBJECTpublic:explicit MyWidget(QWidget *parent = nullptr);~MyWidget();
protected:void paintEvent(QPaintEvent *event);
private slots:void closeWidget();void getImage(const QImage& temp);private:Ui::MyWidget *ui;MyThread* myT;MyThread* myT2;MyThread* myT3;MyThread* myT4;QThread *thread;QImage image;};#endif // MYWIDGET_H
#ifndef SUBWIDGET_H
#define SUBWIDGET_H#include <QWidget>
#include <QImage>
class subwidget : public QWidget
{Q_OBJECT
public:explicit subwidget(QWidget *parent = nullptr);void setImageInfo(QImage e);
protected:void paintEvent(QPaintEvent *e);signals:public slots:
private:QImage img;
};#endif // SUBWIDGET_H
#include "mythread.h"
#include <QPainter>
#include <QPen>
#include <QBrush>
#include <QDebug>
MyThread::MyThread(QObject *parent) : QThread(parent)
{}
void MyThread::drawImage()
{qDebug() << QString("MyThread thread id:") << QThread::currentThreadId();//定义QImage 绘图设备QImage image(500,500,QImage::Format_ARGB32);//定义画家,指定绘图设备QPainter p(&image);//定义5个点QPoint a[]={QPoint (qrand()/500,qrand()/500),QPoint (qrand()/500,qrand()/500),QPoint (qrand()/500,qrand()/500),QPoint (qrand()/500,qrand()/500),QPoint (qrand()/500,qrand()/500),};//画笔QPen pen;pen.setWidth(5);//画笔给画家p.setPen(pen);//定义画刷QBrush brush;brush.setStyle(Qt::SolidPattern);brush.setColor(Qt::blue);p.setBrush(brush);p.drawPolygon(a,5);sleep(5);emit finishImage(image);
}
void MyThread::myt2slot()
{qDebug()<<"myT2 线程所在的槽函数(DirectConnection) thread id ="<< QThread::currentThreadId();
}
void MyThread::myt3slot()
{qDebug()<<"myT3 线程所在的槽函数(QueuedConnection) thread id ="<< QThread::currentThreadId();
}
void MyThread::myt4slot()
{qDebug()<<"myT4 线程所在的槽函数(AutoConnection) thread id ="<< QThread::currentThreadId();
}
#include "mywidget.h"
#include "ui_mywidget.h"
#include <QPainter>
#include <QDebug>
MyWidget::MyWidget(QWidget *parent) :QWidget(parent),ui(new Ui::MyWidget)
{ui->setupUi(this);qDebug() << QString("MyWidget thread id:") << QThread::currentThreadId();//创建子线程myT = new MyThread; //此处无需指定父对象myT2= new MyThread;myT3= new MyThread;myT4= new MyThread;//把自定义模块添加到子线程thread = new QThread(this);myT->moveToThread(thread);//myT2->moveToThread(thread);//启动子线程,并不调用线程处理函数thread ->start();//.htmlconnect(myT,SIGNAL(finishImage(const QImage)),this,SLOT(getImage(const QImage)));connect(thread,SIGNAL(started()),myT2,SLOT(myt2slot()),Qt::DirectConnection);connect(thread,SIGNAL(started()),myT3,SLOT(myt3slot()),Qt::QueuedConnection);connect(thread,SIGNAL(started()),myT4,SLOT(myt4slot()),Qt::AutoConnection);connect(ui->pushButton,SIGNAL(pressed()),myT,SLOT(drawImage()));//connect(this,SIGNAL(destroyed()),this,SLOT(closeWidget())); //这种方式不行?connect(this,&MyWidget::destroyed,this,&MyWidget::closeWidget);
}MyWidget::~MyWidget()
{delete ui;}void MyWidget::getImage(const QImage& temp)
{image=temp;ui->subwidget_cus->setImageInfo(temp);ui->subwidget_cus->update(); //间接调用paintEvent()update();
}
void MyWidget::paintEvent(QPaintEvent *)
{QPainter p(this);p.drawImage(50,50,image);
}void MyWidget::closeWidget()
{printf("close widget...\n");thread->quit();thread->wait();delete myT;
}
#include "subwidget.h"
#include <QPainter>
subwidget::subwidget(QWidget *parent) : QWidget(parent)
{}void subwidget::paintEvent(QPaintEvent *)
{QPainter p(this);p.drawImage(50,50,img);
}
void subwidget::setImageInfo(QImage e)
{img = e;
}
"MyWidget thread id:" 0x59ec
myT2 线程所在的槽函数(DirectConnection) thread id = 0x5b0c
myT3 线程所在的槽函数(QueuedConnection) thread id = 0x59ec
myT4 线程所在的槽函数(AutoConnection) thread id = 0x59ec
"MyThread thread id:" 0x5b0c
更多推荐
Qt笔记(10) qt多线程
发布评论