Qt笔记(9) qt多线程---moveToThread()方式

C++ Qt 同时被 2 个专栏收录
11 篇文章 0 订阅
2 篇文章 0 订阅

介绍了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_OBJECT

public:
    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();

    //http://blog.sina.com.cn/s/blog_a6fb6cc90102vs8z.html
    connect(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

在这里插入图片描述

  • 0
    点赞
  • 1
    评论
  • 0
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

©️2022 CSDN 皮肤主题:游动-白 设计师:我叫白小胖 返回首页

打赏作者

qq_31759493

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值