Search code examples
c++multithreadingqtuser-interfaceboost-thread

Qt MainWindows appears after threads has terminated


I am creating an application using boost::thread (I am aware of QThread) on Qt. I am not using Qt Designer to create my user interface. I have created a class named MainWindow derived it from QWidget. In MainWindow two thread is working reading and processing video.

My main function is

#include<QApplication>
#include<iostream>
#include<mainwindow.h>
#include<opencv2/opencv.hpp>

using namespace cv;
using namespace std;

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

    cout<<"M"<<endl;
        MainWindow m;
    cout<<"H"<<endl;
        m.show();
    return prog.exec();
}

Problem is, my threads are calling functions in which I am using an infinite loop to read frames from disk. So the functions are returning after infinte loops are broken. In the code above first all loops are finished then main window is shown. How can I make main window visible while loops in threads are running.

Note MainWindow is not default class of Qt, it is what I have created manually.


Solution

  • Your form is shown on screen as soon as the event loop of the application has started.

    If you "use" the main thread of the application for performing some blocking tasks (read frames from disk in a loop) then the form cannot be shown.

    The solution here is to let the form to appear on screen and then (once the form has been shown) start your time-consuming activities in other threads (which may communicate to the UI via signal/slot mechanism).

    Anyway, you always speak about boost::thread but you only submit a bunch of code which does not cover that... please share with us other relevant code if you want more help.