Search code examples
c++user-interfaceopencvqt-creatornokia

Playing videos inside QTCreator GUI


I'm currently using QTCreator version 4.7.4 to write a GUI application which plays AVI files inside the application.

The way I'm doing it is by setting each frame as a QPixmap inside a QLabel.

         video.load("lk",video.EXT_AVI);
         if(video.hasLoaded()){
              while(!video.hasFinished()){
                  frame = video.getCurrentFrame();
                  cv::cvtColor(frame, frame, CV_BGR2RGB);
                  QImage myImage = QImage( (const unsigned char*) (frame.data),frame.cols, frame.rows, frame.step1(),QImage::Format_RGB888 );
                  ui->displayLabel->setPixmap((QPixmap::fromImage(myImage)));
                  video.nextFrame();
    }
}

As I'm using the VideoCapture class from the opencv API to get each frame, frames are initially retrieved as CV::MAT objects, so I convert them into QImages.

I'm able to play the files fine if I'm using using the CV::imshow() functions from the opencv API in a simple console program, but when I run the above code in a loop, my program essentially crashes until the last frame, where it will then return to a stable state showing the very last frame.

I'm quite new to C++, but have good experience with Java, so my initial guess for this problem would be that everything was being done on an event thread, and therefore taking up all the resources, causing the app to stop responding for a while.

Any help will be appreciated.

Note: video is my own class, which encapsulates the VideoCapture class from the opencv API.


Solution

  • In short:

    Your code does not update UI during video playback, so program hungs.

    Detailed

    The main problem is that Qt does not update UI just after you make any changes of widget's states in the code. Qt uses events to update widgets. And all events processing in common case run in single thread. Your code is event handler too (probably it is a handler of button click event). Your code changes states of widgets inside a loop, but changes only will be displayed after widgets processed events. But widgets can process events only after current event is processed, that is your code. So, your code really blocks processing until it is finished. And when it is finished, you see last frame.

    What should you do

    You cannot use loop here (ok, you really can, but needn't) You should make a function, that shows one single frame and than finishes to let Qt process events. And Qt should call this function with some fixed interval of time, that is determined from framerate The best way to make Qt call some function with specified time interval is to use a timer.

    So, to start a playback you should initialize video stream ant start a timer. After last frame is shown, you should stop timer.