mythread.h
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include
class MyThread : public QThread
{
Q_OBJECT
public:
MyThread();
~MyThread();
protected:
void run();
signals:
void changeText(QString str);
};
#endif // MYTHREAD_H
widgett.h
#ifndef WIDGETT_H
#define WIDGETT_H
#include
#include "ui_widgett.h"
class WidgetT : public QMainWindow
{
Q_OBJECT
public:
WidgetT(QWidget *parent = 0, Qt::WFlags flags = 0);
~WidgetT();
private:
Ui::WidgetTClass ui;
private slots:
void labelSetText(QString qstr);
};
#endif // WIDGETT_H
mythread.cpp
#include "mythread.h"
MyThread::MyThread()
: QThread()
{
}
MyThread::~MyThread()
{
}
void MyThread::run(){
static int i=0;
while(true)
{
++i;
QString strnum = QString::number(i);
emit changeText(strnum);
QThread::sleep(1);
}
}
widgett.cpp
#include "widgett.h"
#include "mythread.h"
Q_DECLARE_METATYPE(QString);
WidgetT::WidgetT(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
MyThread *mythread = new MyThread;
int id=qRegisterMetaType("");
connect(mythread,SIGNAL(changeText(QString)),this,SLOT(labelSetText(QString)),Qt::QueuedConnection);
mythread->start();
}
WidgetT::~WidgetT()
{
}
void WidgetT::labelSetText(QString qstr){
ui.label->setText(qstr);
}
阅读(2474) | 评论(0) | 转发(0) |