QT多线程通信EMIT

头文件:

#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




阅读更多
个人分类: C++
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭