main.cpp
#include "cat.h"
#include <QCoreApplication>
int main(int argc, char * argv[]) {
QCoreApplication a(argc,argv);
Cat c;
c.name = "A";
c.start(); // 呼叫 run()
Cat c2;
c2.name = "B";
c2.start(); // 呼叫 run()
return a.exec();
}
---------------------------------------------------------------------
Cat.h
#ifndefCAT_H
#defineCAT_H
#include <QThread>
#include <QDebug>
class Cat : public QThread {
public:
Cat();
void run();
QString name;
};
#endif//CAT_H
---------------------------------------------------------------------
Cat.cpp
#include "cat.h"
Cat::Cat(){
}
void Cat::run(){
for(int i=1; i<=100; i++){
qDebug() << name << i;
QThread::currentThread()->msleep(100); // 暫停 0.1 秒
}
}
全站熱搜
留言列表