QVideoWidget not showing on QVBoxLayout with QScrollArea
-
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 window
-
Hi,
Which version of Qt ?
On which OS ?
Does the video show properly if the QVideoWidget is shown standalone ? -
@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.3I
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); };
-
@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();
-
@Pedro-Vicente
if you put widgets into a layout, you do not set position for all widgets in the layout;
video->setFixedSize( 300, 200 ); -
@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);
-
@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
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); }
-
@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. -
@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.
-