Qt工作笔记-对Qt工作线程的进一步理解

运行截图如下:



运行的时候:



停止的时候:




现在一般都是这么搞的

用Qt的事件驱动,用QTime去搞,具体代码如下:

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTimer>
#include <QThread>
#include <QListWidget>

class Worker;

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

public slots:
    void btnClicked();
    void stopBtnClicked();

private:
    Ui::Widget *ui;
    Worker *m_worker1;
    Worker *m_worker2;
    Worker *m_worker3;
    QTimer m_timer;
    QThread thread1;
    QThread thread2;
    QThread thread3;
};

#endif // WIDGET_H

worker.h

#ifndef WORKER_H
#define WORKER_H

#include <QObject>
#include <QListWidget>
#include <QColor>

class Worker:public QObject
{
    Q_OBJECT

public:
    Worker();
    void setListWidget(QListWidget *listWidget);
    void setColor(QColor color);


private slots:
    void onTimeout();

private:
    QListWidget *m_listWidget;
    QColor m_color;
};

#endif // WORKER_H

main.cpp

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

widget.cpp

#include "widget.h"
#include "ui_widget.h"

#include <QDebug>
#include <QThread>
#include <QTimer>
#include <QThread>
#include <QColor>

#include "worker.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowTitle("CSDN IT1995");

    connect(ui->pushButton,SIGNAL(clicked(bool)),this,SLOT(btnClicked()));
    connect(ui->pushButton_2,SIGNAL(clicked(bool)),this,SLOT(stopBtnClicked()));
}


Widget::~Widget()
{
    delete ui;
}

void Widget::btnClicked()
{
    m_worker1=new Worker;
    m_worker1->setListWidget(ui->listWidget);
    m_worker1->setColor(Qt::blue);

    m_worker2=new Worker;
    m_worker2->setListWidget(ui->listWidget);
    m_worker2->setColor(Qt::red);

    m_worker3=new Worker;
    m_worker3->setListWidget(ui->listWidget);
    m_worker3->setColor(Qt::green);

    connect(&m_timer,SIGNAL(timeout()),m_worker1,SLOT(onTimeout()));
    connect(&m_timer,SIGNAL(timeout()),m_worker2,SLOT(onTimeout()));
    connect(&m_timer,SIGNAL(timeout()),m_worker3,SLOT(onTimeout()));
    m_timer.start(1000);

    m_worker1->moveToThread(&thread1);
    m_worker2->moveToThread(&thread2);
    m_worker3->moveToThread(&thread3);

    thread1.start();
    thread2.start();
    thread3.start();
}

void Widget::stopBtnClicked()
{
    m_timer.stop();
    thread1.exit();
    thread2.exit();
    thread3.exit();

    delete m_worker1;
    delete m_worker2;
    delete m_worker3;

}

worker.cpp

#include "worker.h"
#include <QDebug>
#include <QThread>

Worker::Worker()
{

}

void Worker::setListWidget(QListWidget *listWidget)
{
    m_listWidget=listWidget;
}

void Worker::setColor(QColor color)
{
    m_color=color;
}

void Worker::onTimeout()
{
    qDebug()<<"当前的线程ID为:"<<QThread::currentThreadId();
    QListWidgetItem *listItem=new QListWidgetItem("当前的线程ID地址为(int型):"+QString::number((int)QThread::currentThreadId()));
    listItem->setTextColor(m_color);
    m_listWidget->insertItem(0,listItem);
}

猜你喜欢

转载自blog.csdn.net/qq78442761/article/details/80415167