QT线程

 

定义线程类,需要继承QThread

#include <QThread>
#include "QTime"
#include "qDebug"
class TimeThread  : public QThread
{
    Q_OBJECT

public:
    TimeThread(QObject *parent);
    ~TimeThread();
    //默认构造函数
    TimeThread();
protected:
    void run() override;

signals:
    void SendTime123(QString Time);//声明信号函数
};
#include "TimeThread.h"

TimeThread::TimeThread(QObject *parent)
    : QThread(parent)
{}

TimeThread::~TimeThread()
{


}
//默认构造函数
TimeThread::TimeThread()
{


}

void TimeThread::run()
{
    while (1)
    {
        QString time = QTime::currentTime().toString("hh:mm:ss");
        qDebug() << time;
        emit SendTime123(time); //发送信号
        sleep(1);
    }

}

线程对象

   //线程
   TimeThread thread1;

绑定线程的信号对应的槽函数

  connect(&thread1, &TimeThread::SendTime123, this, &QtWidgetsApplication2::ThreadAction);

 

posted @ 2025-11-21 09:46  家煜宝宝  阅读(3)  评论(0)    收藏  举报