简体   繁体   English

从另一个非GUI线程启动QTimer

[英]Start QTimer from another non-gui thread

I try to start QTimer from another thread ( to get better accuracy ). 我尝试从另一个线程启动QTimer(以获得更好的准确性)。

I think that problem is with function connect but let see my code: 我认为问题出在函数连接上,但让我看一下代码:

//code from constructor of parent.cpp class
{
    //run_clock_timer(this); // this works

    std::thread t1{ [this] { run_clock_timer(this); } }; //not works
    t1.join();
}

And function: 和功能:

void class::run_clock_timer(class* inst){
    QTimer * test = new QTimer(inst);
    connect(test, SIGNAL(timeout()), inst, SLOT(updateTime()));
    test->setInterval(50);
    test->start(timer_precision);
}

Debugger told me that code from run_clock_timer was run, but no result. 调试器告诉我run_clock_timer中的代码已运行,但没有结果。

I think i will do it with no problem when i use QThread, but i want to use std::thread. 我认为我使用QThread时会做到这一点,但是我想使用std :: thread。

So whats wrong with my function ? 那我的功能怎么了?

The problem is that QTimer depends on the ability to interact with a running Qt event loop in order to perform its task. 问题在于QTimer依赖于与正在运行的Qt事件循环进行交互以执行其任务的能力。 Since a non-Qt thread has no event loop, the QTimer can't do its job. 由于非Qt线程没有事件循环,因此QTimer无法完成其工作。 (ie there has to be some Qt code running in the thread, that will emit the timeout() signal, that doesn't just happen on its own) (即,必须在线程中运行一些Qt代码,这将发出timeout()信号,而这不仅是单独发生的)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM