solideCode
solideCode

Reputation: 1

how to create a detached thread with QThread, like in std::thread

i've created a thread in QT using QThread but the parent of the thread is exiting before the thread finishes which itself is running infifnitely.

//mainwindow.cpp
void MainWindow::showEvent(QShowEvent *ev)
{
    QMainWindow::showEvent(ev);
    showEventHelper();
}

void MainWindow::showEventHelper()
{
    //back-end thread

    ServerStart *serverstart = new ServerStart();//initializing a pointer to my class
    QThread thread;
    serverstart->moveToThread(&thread);
    QObject::connect(&thread, &QThread::started, serverstart, &ServerStart::run);
    thread.start();

    //in std::thread i used to detache it like so:
    //std::thread worker(serverMain);
    //worker.detach();
}

IMPORTANT: I'm making a GUI project. and my infinite thread is inside an onShow() method that needs to exit in order for the app to continue and make the UI. and I also want to send signals in the future from the thread to the main thread and the main thread should be able to respond and modify the UI according to the signal.

how can i do the same in QT?

Upvotes: 0

Views: 1498

Answers (1)

Ilian Zapryanov
Ilian Zapryanov

Reputation: 1185

You can't, however according to KDAB documentation of proper QThread usage you can emulate such behavior by connecting QThread::finished to QThead::deleteLater as shown from their document for QThread here https://www.kdab.com/wp-content/uploads/stories/multithreading-with-qt-1.pdf

Upvotes: 1

Related Questions