关闭

QT多线程通信EMIT

462人阅读 评论(0) 收藏 举报
分类:

头文件:

#ifndef HEAD_H

#define HEAD_H
#include <QWidget>
#include <QThread>
#include <QMessageBox>
#include <QApplication>
#include <QDebug>

class MyThread: public QThread
{
    Q_OBJECT
public:
    MyThread();
    void run();
signals:
    void send(QString s);
};

class MyWidget : public QWidget {
    Q_OBJECT
public:
    MyWidget (QWidget *parent = 0);
    ~MyWidget();
public slots:
    void receiveslot(QString s);
};
#endif // HEAD_H



CPP文件:#include "head.h"

MyThread::MyThread()
{
}
void MyThread::run()
{
    while(true)
    {
    sleep(5);
    emit send("This is the son thread");
    qDebug()<<"Thread is running!";
    }
//    exec();
    QThread::run();
}


MyWidget::MyWidget(QWidget *parent) :QWidget(parent)
{

}
MyWidget::~MyWidget()
{
}
void MyWidget::receiveslot(QString s)
{
QMessageBox::information(0,"Information",s);
}


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MyWidget w;
    w.show();
    MyThread *mth= new MyThread ;
    QObject::connect(mth,SIGNAL(send(QString)),&w,SLOT(receiveslot(QString)));
    mth->start();
    return a.exec();
}

PRO文件:

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11

TARGET = untitled35


TEMPLATE = app

SOURCES += main.cpp

HEADERS += \
    head.h




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4170次
    • 积分:157
    • 等级:
    • 排名:千里之外
    • 原创:10篇
    • 转载:14篇
    • 译文:0篇
    • 评论:1条
    文章分类
    最新评论