窗口文件cmainwindow.cpp CMainWindow::CMainWindow(QWidget*parent):
QMainWindow(parent),
ui(newUi::CMainWindow)
{
ui->setupUi(this); ....
connect(threadA,SIGNAL(signalUpdateData(QString)),this,SLOT(slotUpdateDate(QString)),Qt::DirectConnection); // 事件同步传递 ... }
voidCMainWindow::slotUpdateDate(QStringtext)
{
qDebug("receivesignalshowMsg");
ui->label->setText(text);
}
线程文件csubthread.cpp:
voidCSubThread::run()
{
qDebug()<<"run";
while(!isStopped)
{
qDebug()<objectName()<<"run...,"<
emitsignalUpdateData(QTime::currentTime().toString());
qDebug("has post signal");
QThread::msleep(2000);
} }
输入出:
"ThreadA" run..., "11:23:45"
receive signal showMsg
has post signal
"ThreadA" run..., "11:23:47"
receive signal showMsg
has post signal