首页 > 编程语言 > 详细

Qt之主线程与子线程通信(linux下)

时间:2014-05-10 04:46:14      阅读:501      评论:0      收藏:0      [点我收藏+]

转载请注明出处:http://blog.csdn.net/feng1790291543 


主线程与子线程通信主要是通过Qt上的G ui按钮,触发使得主线程上的信息发送到子线程中,并且时刻监测线程的运行情况

bubuko.com,布布扣


主线程,头文件:

#ifndef MANAGEWIDGET_H
#define MANAGEWIDGET_H

#include <QtGui/QWidget>
#include <QtCore>
//#include "childwidget.h"
//#include "simplethread.h"
//class ChildWidget;
class SimpleThread;

namespace Ui
{
    class ManageWidget;
}

class ManageWidget : public QWidget
{
    Q_OBJECT

public:
    ManageWidget(QWidget *parent = 0);
    ~ManageWidget();

private:
    Ui::ManageWidget *ui;
    //ChildWidget *child;
    QThread *child;
    SimpleThread *simple;

private slots:
    void on_pushButton_clicked();
    void on_pushButtonRunChild_clicked();
    void on_pushButtonSend_clicked();
    void StartReceive();
    void FinishReceive();

signals:
    void SignalChild(QString str);

};

#endif // MANAGEWIDGET_H

源文件:

#include "managewidget.h"
#include "ui_managewidget.h"
#include "childwidget.h"
#include "simplethread.h"

ManageWidget::ManageWidget(QWidget *parent)
    : QWidget(parent), ui(new Ui::ManageWidget)
{
    ui->setupUi(this);
    simple=new SimpleThread;
    //child=new ChildWidget;
    child=new QThread;
    connect(this,SIGNAL(SignalChild(QString)),simple,SLOT(print_str(QString)));  
    connect(child,SIGNAL(started()),this,SLOT(StartReceive()));
    //connect(child,SIGNAL(finished()),this,SLOT(FinishReceive()));
}

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

void ManageWidget::StartReceive()
{
    qDebug()<<"thread is starting!";
}

void ManageWidget::FinishReceive()
{
    qDebug()<<"thread is finishing!";
}

void ManageWidget::on_pushButtonSend_clicked()
{
    QString str=ui->lineEditContent->text();
    emit SignalChild(str);
    return ;
}

void ManageWidget::on_pushButtonRunChild_clicked()
{
    qDebug()<<"Main Thread Id is "<<QThread::currentThreadId();
    simple->moveToThread(child);
    child->start();
    return ;
}

void ManageWidget::on_pushButton_clicked()
{
    qDebug()<<"first isRuning---->"<<child->isRunning();
    qDebug()<<"first isFinishing---->"<<child->isFinished();
    child->quit();
    child->wait();
    qDebug()<<"second isRuning--->"<<child->isRunning();
    qDebug()<<"second isFinishing---->"<<child->isFinished();
    child->deleteLater();
    delete child;
}

子线程头文件:

#ifndef CHILDWIDGET_H
#define CHILDWIDGET_H

#include <QThread>
#include <QtCore>

class ChildWidget : public QThread
{
     Q_OBJECT
public:
    ChildWidget();
    void run();

};

#endif // CHILDWIDGET_H

子线程源代码:

#include "childwidget.h"

ChildWidget::ChildWidget()
{
}

void ChildWidget::run()
{
    QString str="hello zhangsan";
    qDebug()<<"子线程中的的run() is"<<str;
    exec();
    return ;
}

中间独立线程:

#ifndef SIMPLETHREAD_H
#define SIMPLETHREAD_H

#include <QObject>
#include <QtCore>

class SimpleThread : public QObject
{
     Q_OBJECT
public:
    SimpleThread();
private slots:
    void print_str(QString str);
};

#endif // SIMPLETHREAD_H

源代码:

#include "simplethread.h"

SimpleThread::SimpleThread()
{
}

void SimpleThread::print_str(QString str)
{
    qDebug()<<"Simple Thread is here..."<<str;
    return ;
}

运行效果:

bubuko.com,布布扣

Qt之主线程与子线程通信(linux下),布布扣,bubuko.com

Qt之主线程与子线程通信(linux下)

原文:http://blog.csdn.net/feng1790291543/article/details/25429611

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!