Qt笔记(10) qt多线程

编程入门 行业动态 更新时间:2024-10-07 21:38:07

Qt笔记(10) qt<a href=https://www.elefans.com/category/jswz/34/1767532.html style=多线程"/>

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多线程

本文发布于:2024-02-19 16:07:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1765272.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多线程   笔记   Qt   qt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!