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

Exit video full-screen



  • Hello everyone,
    I am trying to make a basic media player but, i did have a problem with full screen mode for videos
    when i click the fullscreen it shows the video on full screen but when i can't figure out hot to exit the fullscreen mode whene pressing escape key event or double clicked the video.
    can anyone help me please!

    so this my code:

    void lecteur::on_bt_fullscreen_clicked(){
    	if(!mVideoWidget->isFullScreen()){
    		mVideoWidget->setFullScreen(true);
    	}
    	qDebug() << "hi there!";
    }
    

    Thank you


  • Lifetime Qt Champion

    Hi
    Did you try
    mVideoWidget->showNormal() ?
    http://doc.qt.io/qt-5/qwidget.html#showNormal



  • @mrjj Yes i know this method But how can i use it when pressing escape key or double click the video


  • Lifetime Qt Champion

    @HW-Developer
    well for ESC, you would override KeyPressEvent for mVideoWidget
    and respond to it. same with click.
    What type is mVideoWidget ?
    Its it your own child class or just plain Qt one?



  • @mrjj mVideoWidget is a QVideoWidget
    Can you suggest me some examples please, because i'am really a beginner in qt programming
    really thank you


  • Lifetime Qt Champion

    @HW-Developer
    Hi
    You could subclass the QVideoWidget and use your own class instead
    where you react to key and mouse.
    Alternatively there is also event filters
    http://doc.qt.io/qt-5/eventsandfilters.html
    that allow you to do this without subclassing.

    Sample of subclassing and reacting to key / mouse

    #include <QKeyEvent>
    #include <QVideoWidget>
    
    class MyVideoWidget : public QVideoWidget {
     public:
      MyVideoWidget(QWidget* parent = nullptr) : QVideoWidget(parent) { }
     protected:
      virtual void mouseDoubleClickEvent(QMouseEvent* event) override {
        showNormal();
      }
      virtual void keyPressEvent(QKeyEvent* event) override {
        if ( event->key() == Qt::Key_Escape) {
          showNormal();
        }
      }
    };
    


  • @mrjj Thank you so much it works, the only problem left is that the video detach from the MainWindow.
    Do you know why?


  • Lifetime Qt Champion

    @HW-Developer
    Hi
    On some platforms ( Windows) the video is actual an overlay and not really inside the window.
    Im not sure if that is what you are seeing ?
    When you say detach from mainwindow, does it show as its own window or how does it look?
    Im asking as if you dont give a QWidget ( including QVideoWidget) a parent, it will become a window on its own.
    (if you show the code where u create the QVideoWidget, its easy to spot if that is the issue)
    Basically the difference is like
    QVideoWidget *vid= new QVideoWidget;
    versus
    QVideoWidget *vid= new QVideoWidget(this); <<< "this" is the parent



  • @mrjj this is how it shows :
    0_1539619173308_Capture.PNG


  • Lifetime Qt Champion

    @HW-Developer
    Super, i really think its just because your are not assigning a parent when you create it.
    it then becomes a window.
    So instead of replacing the other QVideoWidget you have, it just opens a (new) window.
    Thats easy fixed by assigning a parent.
    Did you based this off some sample i can look at ?



  • @mrjj I make you a simple example, it shows the same issue:

    #include "MyPlayer.h"
    
    MyPlayer::MyPlayer(QWidget *parent)
    	: QMainWindow(parent)
    {
    	ui.setupUi(this);
    	QWidget *zoneCentrale = new QWidget;
    	setCentralWidget(zoneCentrale);
    	player = new QMediaPlayer(this);
    	videoWidget = new MyVideoWidget(this);
    	videoWidget->setGeometry(100, 100, 500, 400);
    	fullScreen = new QPushButton("full-screen", this);
    	layout = new QVBoxLayout();
    	
    	
    	player->setMedia(QUrl::fromLocalFile("C:/Users/ouafa/Downloads/nature.mp4"));
    	player->setVideoOutput(videoWidget);
    	layout->addWidget(videoWidget);
    	layout->addWidget(fullScreen);
    	
    	zoneCentrale->setLayout(layout);
    	player->setVolume(100);
    	setCentralWidget(zoneCentrale);
    	
    	QObject::connect(fullScreen, SIGNAL(clicked()), this, SLOT(on_bt_agrandi_clicked()));
    
    	videoWidget->show();
    	player->play();
    	qDebug()<< player->isVideoAvailable();
    	qDebug() << player->availableMetaData() << player->currentMedia().canonicalUrl();
    }
    void MyPlayer::on_bt_agrandi_clicked() {
    	if (!videoWidget->isFullScreen()) {
    		videoWidget->setFullScreen(true);
    	}
    	
    	qDebug() << "hi there!";
    }
    

  • Lifetime Qt Champion

    Hi
    I could not reproduce
    alt text

    However, using you code 100% as is - made app crash. Not sure why.
    (just tried again to be sure)

    This is the code i used. i rearranged it a bit and gave zoneCentrale directly to the layout.
    else its pretty much the same. Can you try this ? ( change the mp4 file ofc)

     QWidget* zoneCentrale = new QWidget;
      setCentralWidget(zoneCentrale);
      auto player = new QMediaPlayer(this);
      auto videoWidget = new QVideoWidget(this);
      player->setVideoOutput(videoWidget);
    
      auto fullScreen = new QPushButton("full-screen", this);
      auto layout = new QVBoxLayout(zoneCentrale);
    
      player->setMedia(QUrl::fromLocalFile("F:/Dropbox/_qtprojects/wontdelete/test2.mp4"));
    
      layout->addWidget(videoWidget);
      layout->addWidget(fullScreen);
    
      player->setVolume(100);
    
      QObject::connect(fullScreen, SIGNAL(clicked()), this, SLOT(on_bt_agrandi_clicked()));
    
      videoWidget->show();
      player->play();
    


  • @mrjj It's not working, when i make the changes, i run the app and click fullscreen button, it crash, and it blocks the app



  • @mrjj It's working after i verify everything, it is the escape key event, it should be setFullScreen(False) instead of showNormal();

    I really thank you for your help. It was really nice
    Good luck and have a good days


  • Lifetime Qt Champion

    @HW-Developer
    Super.
    Happy days to you too



  • @mrjj @mrjj I'm also facing same problem in Qt C++ 5.12.2 in Red Hat Enterpeise Linux 7.6.
    Please give proper solution if you can...


  • Lifetime Qt Champion

    @RakeshChitte
    Hi
    Issue exiting fullscreen or floating window ?



  • @mrjj
    existing full screen...


  • Lifetime Qt Champion

    @RakeshChitte
    Hi
    Welcome back.
    So what happens when you call showNormal(); ?


Log in to reply