head File:
#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 file:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
TARGET = untitled35
TEMPLATE = app
SOURCES += main.cpp
HEADERS += \
head.h