c++qtqt5qthreadqtimer

QThread with QTimer connection issues


Is there anything wrong with this? It's giving me weird compilation errors:

candidate function not viable: no known conversion from 'void (QThread::*)(QThread::QPrivateSignal)' to 'const char *' for 2nd argument

QTimer timer;
timer.setInterval(3000);
connect(&timer, &QTimer::timeout, this, &ThisClass::runConnectionCheck);
QThread connectionThread;
timer.moveToThread(&connectionThread);
connect(&connectionThread, &QThread::started, &timer, &QTimer::start);
connectionThread.start();

Solution

  • There are 2 QTimer slots called start(), so the compiler has that confusion, so you should QOverload:

    connect(&connectionThread, &QThread::started, &timer, QOverload<>::of(&QTimer::start));
    

    Or static_cast<>():

    connect(&connectionThread, &QThread::started, &timer,static_cast<void (QTimer::*)()>(&QTimer::start));
    

    @KubaOber provides 2 options:

    C++14:

    qOverload<>(&QTimer::start)
    

    Lambda:

    [&]{ timer.start(); }