Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QT OpenCV Play Pause and Stop Video
Forum Updated to NodeBB v4.3 + New Features

QT OpenCV Play Pause and Stop Video

Scheduled Pinned Locked Moved Solved General and Desktop
37 Posts 2 Posters 7.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Stevendragoes
    wrote on last edited by
    #13
    cShowVideo::cShowVideo(Ui::MainWindow& ui, std::string& path, QString& qpath, MainWindow &parent) : MainWindow()
    {
        timer = new QTimer(this);
        connect(timer,SIGNAL(timeout()),this,SLOT(getNextFrame()));
        //ui.textEdit->setText(qpath);
        timer->start(1000);
        DisplayVideo(ui,path,parent);
    

    this is my timer code. It starts properly.
    when i used qDebug, it shows the text in 1 second which means that the Timer function is working?
    However, when I change it to the text edit, as shown below, the text is not appearing as intended
    The code printing is as follows:

    void MainWindow::getNextFrame()
    {
        qDebug("Get Next Frame is working");
        ui->textEdit->append("GetNextFrame");
    }
    
    

    To show its working, the "Get Next Frame is working" is printing every second
    cc2daa6d-75d7-42d1-9596-5e7fe43a2ce7-image.png

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Stevendragoes
      wrote on last edited by
      #14

      @jsulm Sorry forgot to tag you

      jsulmJ 1 Reply Last reply
      0
      • S Stevendragoes

        @jsulm Sorry forgot to tag you

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #15

        @Stevendragoes Do you still have the loop in DisplayVideo?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Stevendragoes
          wrote on last edited by
          #16

          @jsulm Yes I do have the loop, Should I take it out first?

          jsulmJ 1 Reply Last reply
          0
          • S Stevendragoes

            @jsulm Yes I do have the loop, Should I take it out first?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #17

            @Stevendragoes said in QT OpenCV Play Pause and Stop Video:

            Should I take it out first?

            Of course. As I said before this loop is blocking the event loop, so UI does not work properly.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Stevendragoes
              wrote on last edited by
              #18

              @jsulm When I commented out the loop, the timer doesn't run anymore.

              jsulmJ 1 Reply Last reply
              0
              • S Stevendragoes

                @jsulm When I commented out the loop, the timer doesn't run anymore.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #19

                @Stevendragoes Please comment out DisplayVideo call in cShowVideo

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Stevendragoes
                  wrote on last edited by
                  #20

                  I have commented it out also.
                  However, what I want is that when I load the video, the timer to start. So, I initialised the timer in the cShowvideo which is a class inherited from the MainWindow().
                  Is that an incorrect way of doing so?

                  jsulmJ 1 Reply Last reply
                  0
                  • S Stevendragoes

                    I have commented it out also.
                    However, what I want is that when I load the video, the timer to start. So, I initialised the timer in the cShowvideo which is a class inherited from the MainWindow().
                    Is that an incorrect way of doing so?

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #21

                    @Stevendragoes As I said already: in the slot connected to the timer you get next frame and show it. You need to implement this to make it work.

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      Stevendragoes
                      wrote on last edited by
                      #22

                      Yeap. I think I figured it out.
                      I shifted the initialisation of the timer in the MainWindow.

                      MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
                      {
                          timer = new QTimer(this);
                          connect(timer,SIGNAL(timeout()),this,SLOT(getNextFrame()));
                          ui->setupUi(this);
                      }
                      
                      

                      and I used the cShowVideo Class to start the timer when I load the video from the FileDialog

                      cShowVideo::cShowVideo(Ui::MainWindow& ui, std::string& path, QString& qpath, MainWindow &parent) : MainWindow()
                      {
                          parent.timer->start(1000);
                          ui.textEdit->append(qpath);
                      }
                      

                      and it works, it is appending every second.

                      jsulmJ 1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        Stevendragoes
                        wrote on last edited by
                        #23

                        @jsulm
                        From what I saw in the QTimer Class, there is no such thing as pause the timer?

                        1 Reply Last reply
                        0
                        • S Stevendragoes

                          Yeap. I think I figured it out.
                          I shifted the initialisation of the timer in the MainWindow.

                          MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
                          {
                              timer = new QTimer(this);
                              connect(timer,SIGNAL(timeout()),this,SLOT(getNextFrame()));
                              ui->setupUi(this);
                          }
                          
                          

                          and I used the cShowVideo Class to start the timer when I load the video from the FileDialog

                          cShowVideo::cShowVideo(Ui::MainWindow& ui, std::string& path, QString& qpath, MainWindow &parent) : MainWindow()
                          {
                              parent.timer->start(1000);
                              ui.textEdit->append(qpath);
                          }
                          

                          and it works, it is appending every second.

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #24

                          @Stevendragoes This is not good design - child should not access parent members directly. And this timer does not belong to MainWindow but to cShowVideo. Move timer again to cShowVideo.
                          I just noticed that cShowVideo is a local variable in on_actionLoad_Video_triggered(). That means it is destroyed as soon as on_actionLoad_Video_triggered() finishes - that's why your timer is not working. Make sure cShowVideo instance lives as long as it is playing video.
                          Also another note regarding design: your cShowVideo should not know anything about the MainWindow UI. This issue is called tight coupling and makes the maintenance of your software harder. In cShowVideo you should only get next frame and send it to MainWindow via a slot, MainWindow knows better how to use its UI to show the frames.

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          3
                          • S Offline
                            S Offline
                            Stevendragoes
                            wrote on last edited by
                            #25

                            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 program

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

                            ac96b437-ad70-4103-b8b4-efab7b6c279a-image.png

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              Stevendragoes
                              wrote on last edited by
                              #26

                              maybe @J.Hilk could help me?

                              jsulmJ 1 Reply Last reply
                              0
                              • S Stevendragoes

                                maybe @J.Hilk could help me?

                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by
                                #27

                                @Stevendragoes Can you show the destructor of your MainWindow?
                                Also, when closing your app you should first stop the playback if active.

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                0
                                • S Offline
                                  S Offline
                                  Stevendragoes
                                  wrote on last edited by
                                  #28

                                  @jsulm this is my destructor

                                  MainWindow::~MainWindow()
                                  {
                                      cap.release();
                                      delete ui;
                                  }
                                  
                                  
                                  1 Reply Last reply
                                  0
                                  • S Offline
                                    S Offline
                                    Stevendragoes
                                    wrote on last edited by
                                    #29

                                    @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();
                                        }
                                    }
                                    
                                    jsulmJ 1 Reply Last reply
                                    0
                                    • S Stevendragoes

                                      @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();
                                          }
                                      }
                                      
                                      jsulmJ Offline
                                      jsulmJ Offline
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #30

                                      @Stevendragoes I would stop the timer in closeEvent if it is running and user wants to quit.

                                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      1 Reply Last reply
                                      0
                                      • S Offline
                                        S Offline
                                        Stevendragoes
                                        wrote on last edited by
                                        #31

                                        Hi @jsulm I have implemented timer->stop() but it still gives me the same error. I am not too sure why.

                                        1 Reply Last reply
                                        0
                                        • S Offline
                                          S Offline
                                          Stevendragoes
                                          wrote on last edited by
                                          #32

                                          Hi @jsulm
                                          I think i have solved the problem

                                          w.setAttribute(Qt::WA_DeleteOnClose, true);
                                          

                                          this was inside the main.cpp. I am not sure why this happens.

                                          jsulmJ 2 Replies Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved