QT and Video Interfaces



  • Hi All,

    I am developing a personal GUI project to integrate OPENCV with QT. I have successfully integrated using MinGW 7.3.0. My Current GUI looks like this.
    d01b7078-391d-48f6-a37c-5d4fcdcc3bfb-image.png

    I want to do Offline Loading Video and Online Camera Interfaces using two PushButtons.
    I followed this tutorial https://www.youtube.com/watch?v=ZoFm_Mznq1M
    I have created the FileDialog and it is working fine.
    However, I don't want to write "this" and take up the whole screen of my widget. Hence, I created a sizeable widget named "videoframe" (dotted blue rectangle)

    mainwindow.h

    class MainWindow : public QMainWindow
    {
        Q_OBJECT                                    
    
    public:
        QWidget* videoframe;
        Ui::MainWindow *ui;
        std::string path;
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
        QString qpath;
    
        QMediaPlayer* player;
        QVideoWidget* vw;
        QProgressBar* bar;
        QSlider* slider;
    
    private slots:
        void on_actionLoad_Video_triggered();
        void closeEvent(QCloseEvent *event);
        void on_actionPlay_triggered();
        void on_actionPause_triggered();
        void on_actionStop_triggered();
    
    private:
        QString filepath;
    };
    

    mainwindow.cpp

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        player = new QMediaPlayer(this);
        vw = new QVideoWidget(videoframe);
        //to fix the Forcefully exited
        //to fix the Video WIdget
        //To resize and put into the videoframe
    
        player->setVideoOutput(vw);
    
    }
    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();
                cShowVideo(ui, path, qpath);                        //For OPENCV analysis and filtering
                on_actionStop_triggered();                          //Stop the Video
                player->setMedia(QUrl::fromLocalFile(qpath));       //Set the Media
                on_actionPlay_triggered();                          //Play the Media
            }
        }
        else
        {
            ui->textEdit->setText("User Cancelled");
        }
    }
    

    however, when i try to run the program,

    it gives me this error. which means that I failed to cast the video widget into the Qwidget frame? My main motive is to load the video into the QWidget named 'videoframe'.

    Any idea how to do so? Appreciate your help.


  • Qt Champions 2019

    @Stevendragoes said in QT and Video Interfaces:

    it gives me this error

    In which line exactly?



  • @jsulm said in QT and Video Interfaces:

    @Stevendragoes said in QT and Video Interfaces:

    it gives me this error

    In which line exactly?

    Oh the whole Program forcefully fails.. And it can't start the application. The screen compiles and build successfully but it just fails to launch the application.



  • Does it crash when executing inside of QtCreator or outside ?
    If it crashes inside of QtCreator than check the stack trace.
    If it crashes outside of QtCreator than you need to do a deploy.


  • Lifetime Qt Champion

    Hi
    Seems to be a dangling pointer maybe ?
    You have
    QWidget* videoframe;

    which you never set to anything in the code shown.

    You show an ui file so i wonder if you mean

    ui->videoframe
    instead ?

    To use the one you have placed on the UI and not the one you have manually added.



  • This post is deleted!


  • @mranger90 said in QT and Video Interfaces:

    Does it crash when executing inside of QtCreator or outside ?
    If it crashes inside of QtCreator than check the stack trace.
    If it crashes outside of QtCreator than you need to do a deploy.

    Hi @mranger90 it crashed inside the QT Creator when I try to press the run button



  • @mrjj said in QT and Video Interfaces:

    Hi
    Seems to be a dangling pointer maybe ?
    You have
    QWidget* videoframe;

    which you never set to anything in the code shown.

    You show an ui file so i wonder if you mean

    ui->videoframe
    instead ?

    To use the one you have placed on the UI and not the one you have manually added.

    @mrjj yea I want to use the QVideoWidget inside the "videoframe" Qwidget that is created in the Ui shown in dotted rectangle in the UI screen shot. However I don't know how to use link it all together, is it ui->videoframe like you mentioned?



  • @mrjj So should. It be

    vw = new QVideoWidget (ui->videoframe)
    Instead of

    vw= new QVideoWidget(videoframe)?


  • Lifetime Qt Champion

    @Stevendragoes
    Hi
    Yes as the UI hold the widgets you put in visually and
    the other one seems like a dangling pointer which will crash.



  • Hello @mrjj

    I have done the necessary changes. However, the video is not showing in the rectangle frame that I have drawn.



  • Hello @jsulm @mrjj

    I have typed in according to what you have mentioned

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        player = new QMediaPlayer(ui->videowidget);
        vw = new QVideoWidget(ui->videowidget);
        player->setVideoOutput(vw);
    }
    
    

    However, the video does not load when I loaded the video. I have the text box to check the file path. the file path is fine. However, the video could not be displayed in the custom rectangle box that I drew.
    e0ca49fd-8dec-4c34-9e13-a34c8d9e62ad-image.png



  • Hello @jsulm and @mrjj

    After trying out and researching, I found a lazy man's way of showing in the UI, could be called my first draft.

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        player = new QMediaPlayer(this);
        vw = new QVideoWidget(this);
        player->setVideoOutput(vw);
        vw->setGeometry(30,140,800,600);
        vw->show();
    }
    
    

    However, I want to cast into the grid layout as the result wasn't up to my expectations.. I want the videoframe to resize when the window resizes.. Any Idea how? I didn't use the QWidget to load the video. If i were to use the QWidget to load the video, it will be good.


  • Qt Champions 2019

    @Stevendragoes said in QT and Video Interfaces:

    Any Idea how?

    Well, put it into your layout.



  • Alright @jsulm I will go and learn about the layout and do some trial and errors. Any questions I will ask again. Thanks for assisting me


Log in to reply