Qt: 多線程, 就是這么簡單(確實非常簡潔明了)


#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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM