Qt多线程二

19 篇文章 0 订阅

实现功能 :在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();
}

//mythread.h

#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();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值