久久久久久久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 ()?環形?)
主站蜘蛛池模板: xxxcom在线观看 | 久久三区 | 国产欧美在线一区二区 | 国产福利91精品一区二区三区 | 国产成人精品999在线观看 | 一区二区三区观看视频 | 免费精品视频 | 午夜av电影院 | 丁香六月伊人 | 91精品久久久久 | 黄网址在线观看 | 久久久久久久久蜜桃 | 奇米久久 | 久久久国产精品一区 | 韩日av在线 | 欧美偷偷操 | 91电影 | 免费观看的黄色网址 | 欧美日韩精品 | 亚洲美女视频 | 91国产在线视频在线 | 国产成人高清视频 | 亚洲一区亚洲二区 | 成人精品一区亚洲午夜久久久 | 日韩视频在线观看 | 中文字幕91av | 久久精品国产久精国产 | 中文字幕亚洲精品 | 国内精品久久久久 | 欧美精品乱码99久久影院 | 亚洲欧美日韩中文字幕一区二区三区 | 国产欧美精品一区二区 | 国产中文字幕在线观看 | 做a的各种视频 | 欧美mv日韩mv国产网站91进入 | 黄色免费三级 | 欧美在线视频a | 日本不卡一区二区三区在线观看 | 综合久久久久久久 | 久久久久www | 亚洲欧美自拍偷拍视频 |