验证中...
Languages: C/C++
Categories: 桌面应用界面
Latest update 2019-05-20 11:36
mythread.h
Raw Copy
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QObject>
#include <QThread>
class MyThread : public QObject
{
Q_OBJECT
public:
explicit MyThread(QObject *parent = nullptr);
void MyThreadOutTime();
void MyThreadOutTimeParam(const QString &context);
void SetStopFlag(bool flag =true);
signals:
void MyThreadSignal();
void MyThreadSignal(const QString &context);
public slots:
private :
bool isStopFlag;
};
#endif // MYTHREAD_H
mythread.cpp
Raw Copy
#include "mythread.h"
#include <QDebug>
MyThread::MyThread(QObject *parent) : QObject(parent)
{
isStopFlag=false;
}
void MyThread::SetStopFlag(bool flag){
isStopFlag=flag;
}
void MyThread::MyThreadOutTime(){
while(isStopFlag==false){
qDebug()<<"MyThreadOutTime ThreadID :"<<QThread::currentThread();
QThread::sleep(1);
emit MyThreadSignal();
}
}
void MyThread::MyThreadOutTimeParam(const QString &context){
while (isStopFlag==false) {
qDebug()<<"MyThreadOutTimeParam ThreadID :"<<QThread::currentThread();
QThread::sleep(1);
emit MyThreadSignal(context);
}
}
widget.h
Raw Copy
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include "mythread.h"
#include <QThread>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
void closeEvent(QCloseEvent *event);
private slots:
void DisplayData();
void SetTextlayData(const QString &text);
void on_btn_Start_clicked();
void on_btn_Stop_clicked();
signals:
void StartThread();
void StartParamThread(const QString &context);
private:
Ui::Widget *ui;
MyThread *mthread;
QThread *thread;
MyThread *mParamthread;
QThread *ParamThread;
};
#endif // WIDGET_H
widget.cpp
Raw Copy
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QDateTime>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
mthread =new MyThread;
thread=new QThread(this);
mthread->moveToThread(thread);
mParamthread =new MyThread;
ParamThread =new QThread(this);
mParamthread->moveToThread(ParamThread);
/// 注册signal参数类型
qRegisterMetaType<QString>("QString");
void (MyThread::*funSignal)()=&MyThread::MyThreadSignal;
connect(mthread,funSignal,this,&Widget::DisplayData);
void(MyThread::*funParamSiganl)(const QString &)=&MyThread::MyThreadSignal;
connect(mParamthread,funParamSiganl,this,&Widget::SetTextlayData);
/// 通过信号开启线程中的处理函数
connect(this,&Widget::StartThread,mthread,&MyThread::MyThreadOutTime);
connect(this,&Widget::StartParamThread,mParamthread,&MyThread::MyThreadOutTimeParam);
qDebug()<<"Main ThreadID :"<<QThread::currentThread();
}
Widget::~Widget()
{
delete ui;
}
void Widget::SetTextlayData(const QString &text){
QString num =QString("%1: %2 ==> %3").arg(QDateTime::currentDateTimeUtc().toString("HH:mm:ss_zzz")).arg(text).arg(ui->lcdNumber->value());
setWindowTitle(num);
}
void Widget::on_btn_Start_clicked()
{
mthread->SetStopFlag(false);
thread->start();
emit StartThread();
mParamthread->SetStopFlag(false);
ParamThread->start();
emit StartParamThread("LCD");
}
void Widget::on_btn_Stop_clicked()
{
mthread->SetStopFlag(true);
thread->quit();
thread->wait();
mParamthread->SetStopFlag(true);
ParamThread->quit();
ParamThread->wait();
}
void Widget::closeEvent(QCloseEvent *){
on_btn_Stop_clicked();
}
void Widget::DisplayData(){
static int num =0;
num++;
ui->lcdNumber->display(num);
}
1558323336(1).png

Comment list( 0 )

You need to Sign in for post a comment

Help Search