Solved QT OpenCV Play Pause and Stop Video
-
Hello @jsulm
I have FINALLY made it work already. Thank you for your guidance, it was good to drop the cShowVideo Completely. This is the following codes. However, there are a few issues when I try to close the programLoading the Video and Starting the Timer so that it gets the next frame.
void MainWindow::on_actionLoad_Video_triggered() { QMessageBox::StandardButton loadBtn = QMessageBox::question( this, "MainWindow",tr("Load Video? \n"), QMessageBox::No | QMessageBox::Yes,QMessageBox::Yes); if (loadBtn == QMessageBox::Yes) { qpath = QFileDialog::getOpenFileName(this, "Load Video File"); //Input code to skip the event when cancel is pressed if (qpath.isNull()) { ui->textEdit->append("Cannot get file, user cancelled while choosing file"); } else { path = qpath.toLocal8Bit().constData();\ ui->textEdit->append("cShowVideo is called"); //cShowVideo(*ui, path, qpath, *this); //For OPENCV analysis and filtering cap.open(path); vwidth = ui->display_image->height(); vheight = ui->display_image->width(); timer->start(30); //this can set the frame rate cap >> frame; } } else { ui->textEdit->setText("User Cancelled"); } }
This is the getNextFrame() function to get the next frame after every timeout() event from the timer
void MainWindow::getNextFrame() { cap.read(frame); if(frame.empty()==true) return; cv::cvtColor(frame, frame, cv::COLOR_BGR2RGB); cv::resize(frame, frame, cv::Size(vheight,vwidth), 0, 0, cv::INTER_AREA); QImage imdisplay((uchar*)frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888); ui->display_image->setPixmap(QPixmap::fromImage(imdisplay)); }
I have also implemented 3 pushbuttons to play, pause and stop
void MainWindow::on_Play_clicked() { if(timer->isActive() == true){ return; } else { timer->start(30); } } void MainWindow::on_Pause_clicked() { if(timer->isActive() == true) { timer->stop(); } else{ return; } } void MainWindow::on_Stop_clicked() { if(timer->isActive()==true) { timer->stop(); cap.release(); } }
However, when I try to close it, the program finishes unexpectedly and crashed unexpectedly.
This is the screenshot when i try to use the debugger to trace where it went wrong. -
maybe @J.Hilk could help me?
-
@Stevendragoes Can you show the destructor of your MainWindow?
Also, when closing your app you should first stop the playback if active. -
@jsulm this is my destructor
MainWindow::~MainWindow() { cap.release(); delete ui; }
-
@jsulm I have also attached the close event captured for reference
void MainWindow::closeEvent(QCloseEvent *event) //To confirm that the user really wants to quit the application { QMessageBox::StandardButton exitBtn = QMessageBox::question( this, "MainWindow",tr("Are you sure you want to exit?\n"), QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes,QMessageBox::Yes); if (exitBtn == QMessageBox::Yes) { event->accept(); } else { event->ignore(); } }
-
@Stevendragoes I would stop the timer in closeEvent if it is running and user wants to quit.
-
Hi @jsulm I have implemented timer->stop() but it still gives me the same error. I am not too sure why.
-
Hi @jsulm
I think i have solved the problemw.setAttribute(Qt::WA_DeleteOnClose, true);
this was inside the main.cpp. I am not sure why this happens.
-
@Stevendragoes said in QT OpenCV Play Pause and Stop Video:
w.setAttribute
What is w? Maybe it was double delete?
-
@Stevendragoes said in QT OpenCV Play Pause and Stop Video:
w.set
Now I see the problem: w is allocated on the stack and there is no need to delete it explicitly (what Qt::WA_DeleteOnClose does), so it was double delete.
-
@jsulm I suspect is double delete. w is the MainWindow Setup in the main.cpp
The Code is as follows#include "mainwindow.h" #include <QApplication> //Widgets Events Handling, Mouse Movements int main(int argc, char *argv[]) //All Executions are going to begin { QApplication a(argc, argv); //Create the QApplication Object to Handle Events etc. MainWindow w; w.showMaximized(); w.show(); return a.exec(); }
on other hand, do u mind if use my solution to mark it as solved?
-
@jsulm
Thank you so much for your assistance in these few days. Now I need to move on to interfacing with an inbuilt camera or USB connected camera to do my object tracking. -
@Stevendragoes There is no need for Qt::WA_DeleteOnClose, see my previous comment.