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

在 QThread 中啟動 QTimer

Starting QTimer In A QThread(在 QThread 中啟動 QTimer)
本文介紹了在 QThread 中啟動 QTimer的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試在特定線程中啟動 QTimer.但是,計時器似乎沒有執行,也沒有打印出任何內容.是跟定時器、槽還是線程有關?

I am trying to start a QTimer in a specific thread. However, the timer does not seem to execute and nothing is printing out. Is it something to do with the timer, the slot or the thread?

main.cpp

    #include "MyThread.h"
    #include <iostream>
    using namespace std;

    int main(int argc, char *argv[]) {
        MyThread t;
        t.start();
        while(1);
    }

MyThread.h

    #ifndef MYTHREAD_H
    #define MYTHREAD_H

    #include <QTimer>
    #include <QThread>
    #include <iostream>

    class MyThread : public QThread {
        Q_OBJECT
    public:
        MyThread();
    public slots:
        void doIt();
    protected:
        void run();
    };

    #endif  /* MYTHREAD_H */

MyThread.cpp

MyThread.cpp

    #include "MyThread.h"

    using namespace std;

    MyThread::MyThread() {
        moveToThread(this);
    }

    void MyThread::run() {
        QTimer* timer = new QTimer(this);
        timer->setInterval(1);
        timer->connect(timer, SIGNAL(timeout()), this, SLOT(doIt()));
        timer->start();
    }

    void MyThread::doIt(){
        cout << "it works";
    }

推薦答案

正如我所評論的(鏈接中的更多信息),您做錯了:

As I commented (further information in the link) you are doing it wrong :

  1. 您將保存線程數據的對象與另一個對象(負責 doIt())混合在一起.他們應該分開.
  2. 在您的情況下,無需對 QThread 進行子類化.更糟糕的是,您覆蓋了 run 方法,而沒有考慮它在做什么.
  1. You are mixing the object holding thread data with another object (responsible of doIt()). They should be separated.
  2. There is no need to subclass QThread in your case. Worse, you are overriding the run method without any consideration of what it was doing.

這部分代碼應該夠了

QThread* somethread = new QThread(this);
QTimer* timer = new QTimer(0); //parent must be null
timer->setInterval(1);
timer->moveToThread(somethread);
//connect what you want
somethread->start();

現在(Qt 版本 >= 4.7)默認 QThread 在他的 run() 方法中啟動一個事件循環.為了在線程內運行,您只需要移動對象.閱讀文檔...

Now (Qt version >= 4.7) by default QThread starts a event loop in his run() method. In order to run inside a thread, you just need to move the object. Read the doc...

這篇關于在 QThread 中啟動 QTimer的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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在线免费看网址 | 精品福利在线视频 | 国产精品亚洲视频 | 久久不卡日韩美女 | 美女天天操 | 仙人掌旅馆在线观看 | www国产成人免费观看视频,深夜成人网 | 欧美一区二区久久 | 欧美成人第一页 | 亚洲欧美日韩精品久久亚洲区 | 欧美极品一区二区 | 国产成人综合在线 | 色婷婷国产精品综合在线观看 | 伊人在线视频 | 视频一区二区在线观看 | 国产精品成人国产乱 | 国产1区2区3区 | 天天草天天干天天 | 色播久久 | 亚洲精品日韩精品 | 欧美精品一区二区三区四区五区 | 国产区在线观看 | 欧美激情 一区 | 色综合久久久久 | 中文字幕在线观看视频网站 | 一本大道久久a久久精二百 欧洲一区二区三区 | 色吧久久| 亚洲a视频 | 男人的天堂在线视频 | 成人在线一级片 | 国产精品视频在线免费观看 | 亚洲欧美高清 | 欧美一区二区 | 欧美在线 | 亚洲精品乱码8久久久久久日本 | 成在线人视频免费视频 | 天堂三级| 91中文字幕在线观看 | 中文字幕高清一区 | 一区二区三区不卡视频 | 精品国产伦一区二区三区观看说明 |