Chinaunix首页 | 论坛 | 博客
  • 博客访问: 207145
  • 博文数量: 16
  • 博客积分: 371
  • 博客等级: 二等列兵
  • 技术积分: 170
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-09 21:56
文章分类

全部博文(16)

文章存档

2015年(4)

2012年(3)

2011年(9)

分类: 嵌入式

2011-09-26 20:56:22

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);
}
阅读(2409) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~