Qt: 多线程 就是这么简单
2013-03-23 10:28
495 查看
#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)中动态显示数据.
#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)中动态显示数据.
相关文章推荐
- Qt: 多线程, 就是这么简单(确实非常简洁明了)
- 多线程,就是这么简单!
- 理解ERP就是这么简单
- python 多线程就这么简单
- 黑马程序员——多线程原来是这么简单
- 对齐就是这么简单!!!(PowerPoint2010对齐功能)
- std list/vector sort 自定义类的排序就是这么简单
- 模拟登陆CSDN——就是这么简单
- 创造奇迹,就是这么简单!!!
- Cookie实战项目2 记住用户名密码 使用jQuery.Cookie.js 就是这么简单!!!
- python 多线程就这么简单(续)
- Python 多线程就这么简单
- python 多线程就这么简单
- Android Things:树莓派3上手就是这么简单
- Maven就是这么简单
- 炒股其实就是这么简单
- 用户控件的使用就是这么简单!
- 好文转载:90后妹纸学习CPA广告推广 创业就是这么简单
- HTML5边玩边学(8):俄罗斯方块就是这么简单 之 数据模型篇