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.
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.