Qt 以Qobject为基类的多线程实现时间显示

版权声明:DfrY https://blog.csdn.net/dfy1407/article/details/88107742
源程序
1 文件

在这里插入图片描述

2 mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "plctimerthread.h"
#include <QTimer>
#include <QTime>
#include <QDebug>
#include <QString>
#include <QThread>
#include <QDebug>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QThread *thread;
    PlcTimerThread *plcThread;
private slots:
    void on_pushButton_clicked();
    void on_pushButton_2_clicked();
    void on_pushButton_3_clicked();
    void on_pushButton_4_clicked();
    void showTime(QString);
    void on_spinBox_valueChanged();

};

#endif // MAINWINDOW_H

3 plctimerthread.h
#ifndef PLCTIMERTHREAD_H
#define PLCTIMERTHREAD_H

#include <QObject>
#include <QTimer>
#include <QDebug>
#include <QTime>
#include <QString>
#include "ui_mainwindow.h"
#include <QMainWindow>
class PlcTimerThread : public QObject
{
    Q_OBJECT
public:
    explicit PlcTimerThread(QObject *parent = nullptr);
    ~PlcTimerThread();
public:
    void run();
    void dowork();
    void printSpinBox(int);
signals:
    void workStart();
    void workFinished();
    void upDateTime(QString);
private:
    QTimer *timer;
    QString str_time;
    int spinbox;

public slots:
};

#endif // PLCTIMERTHREAD_H

4 main.cpp
#include "mainwindow.h"
#include <QApplication>

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

    return a.exec();
}

5 mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "plctimerthread.h"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    plcThread = new PlcTimerThread();
    thread =new QThread();
    plcThread->moveToThread(thread);

//    connect(thread,&QThread::started,plcThread,&PlcTimerThread::run);
    connect(plcThread,&PlcTimerThread::workStart,plcThread,&PlcTimerThread::run);
    connect(plcThread,&PlcTimerThread::workFinished,thread,&QThread::quit);
    connect(thread,&QThread::finished,thread,&QThread::deleteLater);

    connect(plcThread,&PlcTimerThread::upDateTime,this,&MainWindow::showTime);

    connect(this,&MainWindow::sendSpinbox,plcThread,&PlcTimerThread::printSpinBox);


    thread->start();
    qDebug()<<"开启线程";
}

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


void MainWindow::on_pushButton_clicked()//start
{

    emit plcThread->workStart();
}

void MainWindow::on_pushButton_2_clicked()
{
    qDebug()<<"主线程ID:"<<QThread::currentThread();  //显示当前线程的数值
}

void MainWindow::on_pushButton_3_clicked()//进入线程
{
    plcThread = new PlcTimerThread();
    thread =new QThread();
    plcThread->moveToThread(thread);

//    connect(thread,&QThread::started,plcThread,&PlcTimerThread::run);
    connect(plcThread,&PlcTimerThread::workStart,plcThread,&PlcTimerThread::run);
    connect(plcThread,&PlcTimerThread::workFinished,thread,&QThread::quit);
    connect(thread,&QThread::finished,thread,&QThread::deleteLater);

    connect(plcThread,&PlcTimerThread::upDateTime,this,&MainWindow::showTime);



    thread->start();
    qDebug()<<"开启线程";

}

void MainWindow::on_pushButton_4_clicked()//退出线程
{
    emit plcThread->workFinished();
    qDebug()<<"退出线程";
}
//    thread->quit();

void MainWindow::showTime(QString time)
{
    ui->lineEdit->setText(time);
}

void MainWindow::on_spinBox_valueChanged()
{
    qDebug()<<"changed";
    emit sendSpinbox(ui->spinBox->value());
}





6 plctimerthread.cpp
#include "plctimerthread.h"
#include <QThread>

//static int timerCount = 0;

PlcTimerThread::PlcTimerThread(QObject *parent) : QObject(parent)
{

}

PlcTimerThread::~PlcTimerThread()
{

}

void PlcTimerThread::run()
{
//    emit workStart();
//    timer = new QTimer(this);
//    connect(timer,&QTimer::timeout,this,&PlcTimerThread::dowork);
//    timer->start(1000);
    qDebug()<<"run";
    dowork();
}

void PlcTimerThread::dowork()
{
//    timerCount ++;
//    if (timerCount > 100)
//    {
//        emit workFinished();
//    }

    qDebug()<<QTime::currentTime()<<endl;
    qDebug()<<"显示时间线程ID:"<<QThread::currentThread();  //显示当前线程的数值
    QDateTime time = QDateTime::currentDateTime();
    str_time = time.toString("yyyy-MM-dd hh:mm:ss dddd");
    emit upDateTime(str_time);
    qDebug()<<"spinbox = "<<spinbox;


}

void PlcTimerThread::printSpinBox(int spin)
{
    spinbox = spin;
    qDebug()<<"changed"<<spin;
    qDebug()<<"线程ID:"<<QThread::currentThread();  //显示当前线程的数值
}


7 ui界面

在这里插入图片描述

按钮对应关系
gotoThread->pushButton_3
start->pushButton_1
主线程->pushButton_2
quitThred->pushButton_4

猜你喜欢

转载自blog.csdn.net/dfy1407/article/details/88107742