srdrgkcn89
srdrgkcn89

Reputation: 109

QObject::moveToThread: Widgets cannot be moved to a new thread

My IDE Qt 5.0.1, platform Linux

i have a problem about set widgets to window.(My opinion)

this is my main.cpp->

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QThread cThread;

    MainWindow w;

    w.doSetup(cThread);
    w.moveToThread(&cThread);

    cThread.start();

    if(cThread.isRunning())
    {
        qDebug() << " Thread is Running...";
    }


    w.show();

    return a.exec();
}

this is doSetup() method->

void MainWindow::doSetup(QThread &mainThread)
{
    QObject::connect(&mainThread, &QThread::started, this, &MainWindow::activeLoopMainC);
}

i checked my signal-slot mechanism and it works.

slot method->

void MainWindow::activeLoopMainC()
{
    qDebug() << " Signal-Slot structure working successfully..";
    mainThreadProc((void*)(instAddr));
}

i call a function from my main.c by this slot method.

In debugging there is no problem about working codes. But my window is blank. there is only frame.

i receive an error message: QObject::moveToThread: Widgets cannot be moved to a new thread

How can i solve this problem?

Thank you in advance for your answers.

Upvotes: 7

Views: 9775

Answers (1)

Nemanja Boric
Nemanja Boric

Reputation: 22157

You can't move widgets into another thread - in order to keep user interface responsive, Qt needs to do all GUI work inside main thread.

If you have background work to do, then move background worker to other thread, and not the user interface.

Upvotes: 14

Related Questions