久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

帶有 movetothread 的 qt 線程

qt thread with movetothread(帶有 movetothread 的 qt 線程)
本文介紹了帶有 movetothread 的 qt 線程的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試使用線程創建程序:主要以一個循環開始.當測試返回 true 時,我創建一個對象,并希望該對象在另一個線程中工作然后返回并開始測試.

I'm trying to create a program using threads: the main start with a loop. When a test returns true, I create an object and I want that object to work in an other thread then return and start the test .

QCoreApplication a(argc, argv);
while(true){
    Cmd cmd;
    cmd =db->select(cmd);
    if(cmd.isNull()){ 
        sleep(2);  
        continue ;
    }
    QThread *thread = new QThread( );
    process *class= new process ();
    class->moveToThread(thread);
    thread->start();

    qDebug() << " msg"; // this doesn't run until  class finish it's work 
}
return a.exec();

問題是當我啟動新線程時,主線程停止并等待新線程完成.

the problem is when i start the new thread the main thread stops and wait for the new thread's finish .

推薦答案

規范的 Qt 方式如下所示:

The canonical Qt way would look like this:

 QThread* thread = new QThread( );
 Task* task = new Task();

 // move the task object to the thread BEFORE connecting any signal/slots
 task->moveToThread(thread);

 connect(thread, SIGNAL(started()), task, SLOT(doWork()));
 connect(task, SIGNAL(workFinished()), thread, SLOT(quit()));

 // automatically delete thread and task object when work is done:
 connect(task, SIGNAL(workFinished()), task, SLOT(deleteLater()));
 connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));

 thread->start();

如果您不熟悉信號/插槽,Task 類將如下所示:

in case you arent familiar with signals/slots, the Task class would look something like this:

class Task : public QObject
{
Q_OBJECT
public:
    Task();
    ~Task();
public slots:
    // doWork must emit workFinished when it is done.
    void doWork();
signals:
    void workFinished();
};

這篇關于帶有 movetothread 的 qt 線程的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 五月婷婷激情 | 亚洲精品成人 | 国产精品三级 | 久久综合一区 | 久久爱黑人激情av摘花 | 羞羞视频免费观 | 黄色在线免费观看 | 91精品国产91久久久久久最新 | 中文字幕一区二区三区在线观看 | 久久这里有精品 | 国产精品99视频 | 国产伦精品一区二区三毛 | 成人在线精品 | 瑟瑟视频在线看 | 三级av在线 | 日韩欧美二区 | 亚洲国产成人精品女人 | 国产成人精品网站 | 99久久影院 | 亚洲国产福利视频 | 日本小视频网站 | 久久久久久毛片免费观看 | 日本精品一区二区 | 天天夜碰日日摸日日澡 | 国产精品色| 91精品国产色综合久久不卡98口 | 国产一区二区三区在线 | 午夜影院在线观看 | 99re国产视频| 超碰av在线 | 精品视频一区二区三区在线观看 | 欧美精品一区二区三区四区五区 | 久久网国产 | 黄免费在线 | 国产日韩中文字幕 | 国产一区二区三区视频 | 国产精品久久久久久久岛一牛影视 | 91电影院| 国产一区二区三区免费观看视频 | 99精品久久久久 | 自拍偷拍小视频 |