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. QVideoWidget not showing on QVBoxLayout with QScrollArea

QVideoWidget not showing on QVBoxLayout with QScrollArea

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 1.3k Views 2 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.
  • P Offline
    P Offline
    Pedro Vicente
    wrote on last edited by Pedro Vicente
    #1

    I have a scrollable widget that hosts many widgets on a vertical layout

    Trying to add a QVideoWidget, it is not displayed .
    This sample reproduces the issue
    Thanks for any help

    #include <QApplication>
    #include <QMainWindow>
    #include <QObject>
    #include <QImage>
    #include <QNetworkAccessManager>
    #include <QNetworkReply>
    #include <QTextEdit>
    #include <QLabel>
    #include <QMediaPlayer>
    #include <QVideoWidget>
    #include <QAudioOutput>
    #include <QMediaMetaData>
    #include <QVBoxLayout>
    #include <QScrollArea>
    
    int main(int argc, char** argv)
    {
      QApplication app(argc, argv);
    
      QWidget* central = new QWidget;
      QScrollArea* scroll = new QScrollArea;
      QVBoxLayout* layout = new QVBoxLayout(central);
      scroll->setWidget(central);
      scroll->setWidgetResizable(true);
    
      int i = 0;
      while (i < 2)
      {
        QString str = "\U0001F600";
        QTextEdit* text = new QTextEdit();
        text->setReadOnly(true);
        text->setGeometry(0, 0, 100, 100);
        text->setText(str);
        text->show();
        layout->addWidget(text);
        i++;
      }
    
      
      QMediaPlayer* player = new QMediaPlayer;
      QAudioOutput* audio = new QAudioOutput;
      player->setAudioOutput(audio);
      QUrl url_media("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4");
      player->setSource(url_media);
      QVideoWidget* video = new QVideoWidget();
      player->metaData().value(QMediaMetaData::Resolution).toSize();
      video->setGeometry(0, 0, 300, 200);
      player->setVideoOutput(video);
      video->show();
      audio->setVolume(50);
      player->play();
    
      layout->addWidget(video);
    
      QMainWindow* w = new QMainWindow;
      w->setGeometry(50, 50, 480, 320);
      w->setCentralWidget(scroll);
      w->show();
    
      return app.exec();
    }
    

    The video starts playing, just now showing in any windowUntitled.png

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Which version of Qt ?
      On which OS ?
      Does the video show properly if the QVideoWidget is shown standalone ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      P 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Which version of Qt ?
        On which OS ?
        Does the video show properly if the QVideoWidget is shown standalone ?

        P Offline
        P Offline
        Pedro Vicente
        wrote on last edited by Pedro Vicente
        #3

        @SGaist said in QVideoWidget not showing on QVBoxLayout with QScrollArea:

        Does the video show properly if the QVideoWidget is shown standalone ?

        Yes

        In this sample, without any layout and scroll code , it does show

        I don't think it's platform or QT version dependent
        Tried on Windows, Mac, with 6.70 and 6.6.3

        IUntitled.png

        It seems just a basic error in my code (I have been away from QT coding for a while, this is a recent comeback)

        As always, thanks for the quick feedback !

        mainwindow.cpp

        MainWindow::MainWindow()
        {
          QString str = "\U0001F600";
          QTextEdit* text = new QTextEdit(this);
          text->setReadOnly(true);
          text->setGeometry(0, 0, 100, 100);
          text->setText(str);
          text->show();
        
          network = new QNetworkAccessManager(this);
          connect(network, &QNetworkAccessManager::finished, this, &MainWindow::on_finished);
          QUrl url("http://100.36.4.152/image/image.jpg");
          QNetworkRequest request(url);
          network->get(request);
          label = new QLabel(this);
        
          QMediaPlayer* player = new QMediaPlayer;
          QAudioOutput* audio = new QAudioOutput;
          player->setAudioOutput(audio);
         QUrl url_media("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4");
          player->setSource(url_media);
          QVideoWidget* video = new QVideoWidget(this);
          player->metaData().value(QMediaMetaData::Resolution).toSize();
          video->setGeometry(700, 0, 300, 200);
          player->setVideoOutput(video);
          video->show();
          audio->setVolume(50);
          player->play();
        
        }
        
        ///////////////////////////////////////////////////////////////////////////////////////
        //MainWindow::on_finished
        ///////////////////////////////////////////////////////////////////////////////////////
        
        void MainWindow::on_finished(QNetworkReply* reply)
        {
          QByteArray data = reply->readAll();
          image.loadFromData(data);
          label->setPixmap(QPixmap::fromImage(image));
          label->setGeometry(0, 100, image.width(), image.height());
          emit loaded();
        }
        
        
        

        header file

        
        class MainWindow : public QMainWindow
        {
          Q_OBJECT
        public:
          MainWindow();
        
        protected:
          QImage image;
          QNetworkAccessManager* network;
          QLabel* label;
        
        signals:
          void loaded();
        
        protected slots:
          void on_finished(QNetworkReply* reply);
        
        };
        
        P 1 Reply Last reply
        0
        • P Pedro Vicente

          @SGaist said in QVideoWidget not showing on QVBoxLayout with QScrollArea:

          Does the video show properly if the QVideoWidget is shown standalone ?

          Yes

          In this sample, without any layout and scroll code , it does show

          I don't think it's platform or QT version dependent
          Tried on Windows, Mac, with 6.70 and 6.6.3

          IUntitled.png

          It seems just a basic error in my code (I have been away from QT coding for a while, this is a recent comeback)

          As always, thanks for the quick feedback !

          mainwindow.cpp

          MainWindow::MainWindow()
          {
            QString str = "\U0001F600";
            QTextEdit* text = new QTextEdit(this);
            text->setReadOnly(true);
            text->setGeometry(0, 0, 100, 100);
            text->setText(str);
            text->show();
          
            network = new QNetworkAccessManager(this);
            connect(network, &QNetworkAccessManager::finished, this, &MainWindow::on_finished);
            QUrl url("http://100.36.4.152/image/image.jpg");
            QNetworkRequest request(url);
            network->get(request);
            label = new QLabel(this);
          
            QMediaPlayer* player = new QMediaPlayer;
            QAudioOutput* audio = new QAudioOutput;
            player->setAudioOutput(audio);
           QUrl url_media("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4");
            player->setSource(url_media);
            QVideoWidget* video = new QVideoWidget(this);
            player->metaData().value(QMediaMetaData::Resolution).toSize();
            video->setGeometry(700, 0, 300, 200);
            player->setVideoOutput(video);
            video->show();
            audio->setVolume(50);
            player->play();
          
          }
          
          ///////////////////////////////////////////////////////////////////////////////////////
          //MainWindow::on_finished
          ///////////////////////////////////////////////////////////////////////////////////////
          
          void MainWindow::on_finished(QNetworkReply* reply)
          {
            QByteArray data = reply->readAll();
            image.loadFromData(data);
            label->setPixmap(QPixmap::fromImage(image));
            label->setGeometry(0, 100, image.width(), image.height());
            emit loaded();
          }
          
          
          

          header file

          
          class MainWindow : public QMainWindow
          {
            Q_OBJECT
          public:
            MainWindow();
          
          protected:
            QImage image;
            QNetworkAccessManager* network;
            QLabel* label;
          
          signals:
            void loaded();
          
          protected slots:
            void on_finished(QNetworkReply* reply);
          
          };
          
          P Offline
          P Offline
          Pedro Vicente
          wrote on last edited by
          #4

          @Pedro-Vicente said in QVideoWidget not showing on QVBoxLayout with QScrollArea:

          QVideoWidget* video = new QVideoWidget(this);

          To note that in the no layout version, the widgets are added as children of the main window ( "this" as argument)

          QVideoWidget* video = new QVideoWidget(this);
          

          without it , the video displays as an additional new window (that cannot be resized or moved)

          QVideoWidget* video = new QVideoWidget();
          
          JoeCFDJ 1 Reply Last reply
          0
          • P Pedro Vicente

            @Pedro-Vicente said in QVideoWidget not showing on QVBoxLayout with QScrollArea:

            QVideoWidget* video = new QVideoWidget(this);

            To note that in the no layout version, the widgets are added as children of the main window ( "this" as argument)

            QVideoWidget* video = new QVideoWidget(this);
            

            without it , the video displays as an additional new window (that cannot be resized or moved)

            QVideoWidget* video = new QVideoWidget();
            
            JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on last edited by
            #5

            @Pedro-Vicente
            if you put widgets into a layout, you do not set position for all widgets in the layout;
            video->setFixedSize( 300, 200 );

            P 2 Replies Last reply
            0
            • JoeCFDJ JoeCFD

              @Pedro-Vicente
              if you put widgets into a layout, you do not set position for all widgets in the layout;
              video->setFixedSize( 300, 200 );

              P Offline
              P Offline
              Pedro Vicente
              wrote on last edited by
              #6

              @JoeCFD said in QVideoWidget not showing on QVBoxLayout with QScrollArea:

              video->setFixedSize( 300, 200 );

              yes, that was it!
              thanks

               //video->setGeometry(0, 0, 300, 200);
               video->setFixedSize(300, 200);
              
              JoeCFDJ 1 Reply Last reply
              0
              • JoeCFDJ JoeCFD

                @Pedro-Vicente
                if you put widgets into a layout, you do not set position for all widgets in the layout;
                video->setFixedSize( 300, 200 );

                P Offline
                P Offline
                Pedro Vicente
                wrote on last edited by
                #7

                @JoeCFD said in QVideoWidget not showing on QVBoxLayout with QScrollArea:

                @Pedro-Vicente
                if you put widgets into a layout, you do not set position for all widgets in the layout;
                video->setFixedSize( 300, 200 );

                There is still one issue, the scroll does now show in this sample

                Untitled.png

                MainWindow::MainWindow()
                {
                  QWidget* central = new QWidget;
                  QScrollArea* scroll = new QScrollArea;
                  QVBoxLayout* layout = new QVBoxLayout(scroll);
                  scroll->setWidget(central);
                  scroll->setWidgetResizable(true);
                
                  QString str = "\U0001F600";
                  QTextEdit* text = new QTextEdit();
                  text->setReadOnly(true);
                  text->setGeometry(0, 0, 100, 100);
                  text->setText(str);
                  text->show();
                
                  network = new QNetworkAccessManager(this);
                  connect(network, &QNetworkAccessManager::finished, this, &MainWindow::on_finished);
                  QUrl url("http://100.36.4.152/image/image.jpg");
                  QNetworkRequest request(url);
                  network->get(request);
                  label = new QLabel();
                
                  QMediaPlayer* player = new QMediaPlayer;
                  QAudioOutput* audio = new QAudioOutput;
                  player->setAudioOutput(audio);
                  QUrl url_media("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4");
                  player->setSource(url_media);
                  QVideoWidget* video = new QVideoWidget();
                  player->metaData().value(QMediaMetaData::Resolution).toSize();
                  video->setFixedSize(300, 200);
                  player->setVideoOutput(video);
                  video->show();
                  audio->setVolume(50);
                  player->play();
                
                  layout->addWidget(text);
                  layout->addWidget(label);
                  layout->addWidget(video);
                
                  setCentralWidget(scroll);
                }
                
                1 Reply Last reply
                0
                • P Pedro Vicente

                  @JoeCFD said in QVideoWidget not showing on QVBoxLayout with QScrollArea:

                  video->setFixedSize( 300, 200 );

                  yes, that was it!
                  thanks

                   //video->setGeometry(0, 0, 300, 200);
                   video->setFixedSize(300, 200);
                  
                  JoeCFDJ Offline
                  JoeCFDJ Offline
                  JoeCFD
                  wrote on last edited by JoeCFD
                  #8

                  @Pedro-Vicente the same thing for all widgets and better to set parents to widgets as well. Otherwise, you may have memory leak.
                  setGeometry( x, y, width, height ) call is wrong because it sets position and size.

                  jsulmJ 1 Reply Last reply
                  0
                  • JoeCFDJ JoeCFD

                    @Pedro-Vicente the same thing for all widgets and better to set parents to widgets as well. Otherwise, you may have memory leak.
                    setGeometry( x, y, width, height ) call is wrong because it sets position and size.

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

                    @JoeCFD said in QVideoWidget not showing on QVBoxLayout with QScrollArea:

                    Otherwise, you may have memory leak.

                    No, if a widget is added to a layout the layout takes the ownership.

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

                    JoeCFDJ 1 Reply Last reply
                    2
                    • jsulmJ jsulm

                      @JoeCFD said in QVideoWidget not showing on QVBoxLayout with QScrollArea:

                      Otherwise, you may have memory leak.

                      No, if a widget is added to a layout the layout takes the ownership.

                      JoeCFDJ Offline
                      JoeCFDJ Offline
                      JoeCFD
                      wrote on last edited by
                      #10

                      @jsulm True. Thanks.

                      1 Reply Last reply
                      0
                      • P Pedro Vicente referenced this topic on

                      • Login

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