实现功能 :在APP 上除了main 线程外(GUI线程),还有两个线程a,b,通过keyA,B来在终端显示内容
//thread.h
#ifndef THREAD_H
#define THREAD_H
//mysel definite thread header
#include<QThread>
//definite a Thread Class
class Thread:public QThread //thread inherit QThread
{
Q_OBJECT
public:
Thread();
void setMessage(const QString &message);//member function
void stop();
protected:
virtual void run();//void run();ye xing de
private:
QString messageStr;
volatile bool stop_flag;//many thread visit it ,so it is a volatile type
};//
#endif // THREAD_H
//thread.cpp
#include <QtCore>
#include <iostream>
#include <QDebug>
#include"thread.h"
//initial construct function
Thread::Thread()
{
stop_flag=false;
}
void Thread::run()
{
// if(!stop_flag)
while(!stop_flag)//continus print context
{
// sleep(1);//add
std::cerr<<qPrintable(messageStr);//in terminate print
sleep(1);//add sleep 1s
}
stop_flag=false;
std::cerr << std::endl;// enter fu
}
void Thread::stop()
{
stop_flag=true;//set 1 stop
}
void Thread::setMessage(const QString &message)
{
messageStr=message;
}
//main.cpp
#include <QtGui/QApplication>
#include "mythread.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
mythread w;
w.show();
return a.exec();
}
#ifndef MYTHREAD_H
#define MYTHREAD_H
// mythread.h is use display--- dialog
#include <QWidget>
#include <QPushButton>
#include"thread.h"
namespace Ui {
class mythread;
}
class mythread : public QWidget {
Q_OBJECT
public:
mythread(QWidget *parent = 0);
~mythread();
public slots:
void AThread_StartOrStop();//thread a stop and start shift
void BThread_StartOrStop();//thread b stop and start shift
protected:
void changeEvent(QEvent *e);
//ADD
void closeEvent(QCloseEvent *event);
private:
Ui::mythread *ui;
Thread thread_a;//#include"thread.h"
Thread thread_b;
QPushButton *pBn_thread_a;
QPushButton *pBn_thread_b;
QPushButton *pBn_quit;
};
#endif // MYTHREAD_H
//mythread.cpp
#include "mythread.h"
#include "ui_mythread.h"
#include<QHBoxLayout>
mythread::mythread(QWidget *parent) :
QWidget(parent),
ui(new Ui::mythread)
{
ui->setupUi(this);
this->resize(320,240);
thread_a.setMessage("Aa");//a thread will print context
thread_b.setMessage("Bb");//b thread will print context
pBn_thread_a=new QPushButton(tr("Start A"));
pBn_thread_b=new QPushButton(tr("Start B"));
pBn_quit=new QPushButton(tr("Quit"));
//disply pushbutton,here is use 布局管理器 QHBoxLayout
/* pBn_thread_a->show();
pBn_thread_b->show();
pBn_quit->show();
*/
pBn_quit->setDefault(true);//not print context
//signal and slots
connect(this->pBn_thread_a,SIGNAL(clicked()),this,SLOT(AThread_StartOrStop()));
connect(this->pBn_thread_b,SIGNAL(clicked()),this,SLOT(BThread_StartOrStop()));
connect(this->pBn_quit,SIGNAL(clicked()),this,SLOT(close()));
//disply pushbutton,here is use 布局管理器 QHBoxLayout
QHBoxLayout *layout=new QHBoxLayout;//水平位置
layout->addWidget(pBn_thread_a);
layout->addWidget(pBn_thread_b);
layout->addWidget(pBn_quit);
//
this->setLayout(layout);
this->setWindowTitle("this is my thread test");
}
mythread::~mythread()
{
delete ui;
}
void mythread::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void mythread::AThread_StartOrStop()
{
if(thread_a.isRunning())
{
thread_a.stop();
pBn_thread_a->setText("Start A");
}
else
{
thread_a.start();//so call run fuction
pBn_thread_a->setText("Stop A");
}
}
void mythread::BThread_StartOrStop()
{
if(thread_b.isRunning())
{
thread_b.stop();
pBn_thread_b->setText("Start B");
}
else
{
thread_b.start();//so call run fuction
pBn_thread_b->setText("Stop B");
}
}
//CLOSE thread
void mythread::closeEvent(QCloseEvent *event)
{
this->event
thread_a.stop();
thread_b.stop();
thread_a.wait();
thread_b.wait();
// QThread::wait(this);
// event->accept();
}