码迷,mamicode.com
首页 > 编程语言 > 详细

Qt: 多线程, 就是这么简单(确实非常简洁明了)

时间:2016-10-20 00:28:31      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

#include <iostream>
#include <QApplication>
#include <QThread>
#include <QString>

class Thread : public QThread {
public:
Thread(QString name = "") {
stopped = false;
this->name = name;
}

void run() {
while (!stopped) {
std::cout << "In " << name.toStdString() << "‘s run()." << std::endl;
QThread::msleep(400);
}
}

void stop() {
stopped = true;
}

private:
volatile bool stopped;
QString name;
};

int main(int argc, char *argv[]) {
QApplication app(argc, argv);

Thread thread;
thread.start();
Thread thread1("Thread1");
thread1.start();
Thread thread2("Thread2");
thread2.start();

return app.exec();
}

在Widget中, 还可以使用如在继承自QObject 的 void showEvent(QShowEVent *event)中使用myTimerId = startTimer();
在void hideEvent(QHideEVent *event)中使用killTimer(myTimerId);
在void timerEvent(QTimerEvent *event)中更新数据
在void paintEvent(QPaintEvent *event)中动态显示数据.

 

http://www.cppblog.com/biao/archive/2008/03/21/45053.html

Qt: 多线程, 就是这么简单(确实非常简洁明了)

标签:

原文地址:http://www.cnblogs.com/findumars/p/5979168.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!