简体   繁体   中英

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?

您不能,但是根据正确QThread使用的 KDAB 文档,您可以通过将QThread::finished连接到QThead::deleteLater来模拟这种行为,如他们的QThread文档所示https://www.kdab.com/wp-内容/上传/故事/multithreading-with-qt-1.pdf

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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