Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to add Qvideowidget to second mainwindow



  • QVideoWidget *vw = new QVideoWidget() ;
    
    QMediaPlayer * mp = new QMediaPlayer(vw);
    
        QString vidstring = QFileDialog::getOpenFileName(this, "Select video file", QDir::homePath());
    
        mp->setMedia(QUrl::fromLocalFile(vidstring));
        mp->setVideoOutput(vw);
        vw->setGeometry(100, 100, 500, 500);
        vw->show();
        mp->play();
    
    
    }
    Am having two classes one contain the main
     window while the other also has it own window, 
    whenever I play the video the Qvideowiget has
     it own window, I want to display the QVideowidget 
    inside the second windows, but all my effect has not
     been fruitful... thanks in advance ```


  • i tried this and it worked i made the QVideowidget the child of the second window and it worked...

    QVideoWidget *vw = new QVideoWidget() ;
    
    QMediaPlayer * mp = new QMediaPlayer(vw);
    
        QString vidstring = QFileDialog::getOpenFileName(this, "Select video file", QDir::homePath());
    
        mp->setMedia(QUrl::fromLocalFile(vidstring));
        mp->setVideoOutput(vw);
        vw->setGeometry(100, 100, 500, 500);
      vw->setParent(this);
        vw->show();
        mp->play();
    
    
    }
    

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What is that second MainWindow base class ?

    In any case, you should take a look at the Layout Management chapter in Qt's documentation.



  • @SGaist the second window base class is QWidget pls, i will pass through the documentation soon


  • Lifetime Qt Champion

    Put a layout on that widget and add your video player to it.



  • @SGaist it works but unfortunately that was not what i was thinking about,

    int main(int argc, char *argv[])
    {
    	QApplication a(argc, argv);
    	QApplication1 w;
    	QVideoWidget vw(&w);
    	vw.setGeometry(20, 20, 120, 140);
    	QMediaPlayer * mp = new QMediaPlayer(&w);
    
    		QString videoname = QFileDialog::getOpenFileName( &w,"choose video file", QDir::homePath());
    	  mp->setMedia(QUrl::fromLocalFile(videoname));
    	  mp->setVideoOutput(&vw);
    	     mp->play();
    	    w.setCentralWidget(&vw);
    
    	w.show();
    	return a.exec();
    }
    with the code above,  i just create an object of the main class and i passed the Qvideowidget to it
    but i cant do same to the other class...  so this code adds the Qvideowidget to the main Window form.. i dont know if am been understood

  • Lifetime Qt Champion

    Why don't you create that QVideoWidget directly in that second MainWindow ?

    By the way, did you really named your class QApplication1 ? If so, that's a very bad idea.



  • @SGaist that ia what i wanted to do, if you can help, the QApplication1 is just a test class nothing serious


  • Lifetime Qt Champion

    Then go to the constructor of that class and add your video widget related code there.

    Even if nothing serious you should rather use FooClass than deriving names from one of Qt's own classes. You would be surprise by how fast things get serious when developing.



  • @SGaist still the same result, no difference


  • Lifetime Qt Champion

    Did you set your video player in a layout that you set on your secondary MainWindow all within the constructor of that class ?



  • @SGaist 0_1558300851631_Screenshot (10).png i want the video inside the right window


  • Lifetime Qt Champion

    Did you do exactly how I suggested you to do ?

    Can you show the actual code you are using ?



  • @SGaist thanks for your time, this are the code

    QWidget *window = new QWidget;
    	QFormLayout *layout = new QFormLayout;
    	QVideoWidget * vw = new QVideoWidget();
    	QMediaPlayer * mp = new QMediaPlayer(vw);
    	 QPushButton *PAUSE = new QPushButton("Pause");
    	 QString vidstring = QFileDialog::getOpenFileName(this, "Select video file", QDir::homePath());
    	
    	 //layout->addWidget(PAUSE);
    	 mp->setMedia(QUrl::fromLocalFile(vidstring));
    	 mp->setVideoOutput(vw);
    	  layout->addWidget(vw);
    	 vw->setGeometry(100, 100, 500, 500);
    	 vw->show();
    	 vw->setAttribute(Qt::WA_DeleteOnClose);
    	 window->setLayout(layout);
    	 mp->play();
    	 window->show();
    


  • @Allaye probably my problem is how can i call the second window and set the QVideowidget to display on it rather than displaying on it own... thanks



  • i tried this and it worked i made the QVideowidget the child of the second window and it worked...

    QVideoWidget *vw = new QVideoWidget() ;
    
    QMediaPlayer * mp = new QMediaPlayer(vw);
    
        QString vidstring = QFileDialog::getOpenFileName(this, "Select video file", QDir::homePath());
    
        mp->setMedia(QUrl::fromLocalFile(vidstring));
        mp->setVideoOutput(vw);
        vw->setGeometry(100, 100, 500, 500);
      vw->setParent(this);
        vw->show();
        mp->play();
    
    
    }
    

Log in to reply