Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 214
srdrgkcn89 Avatar asked Aug 15 '13 14:08

srdrgkcn89


People also ask

How to move widgets from one thread to another?

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.

Is it possible to delete a QObject from another thread?

Calling delete on a QObject from a thread other than the one that owns the object (or accessing the object in other ways) is unsafe, unless you guarantee that the object isn't processing events at that moment.

What does the function movetothread () do?

The QObject::moveToThread () function changes the thread affinity for an object and its children (the object cannot be moved if it has a parent).

What is the difference between QObject and qthread?

The child of a QObject must always be created in the thread where the parent was created. This implies, among other things, that you should never pass the QThread object ( this) as the parent of an object created in the thread (since the QThread object itself was created in another thread). Event driven objects may only be used in a single thread.


1 Answers

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.

like image 118
Nemanja Boric Avatar answered Oct 19 '22 23:10

Nemanja Boric