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

Add Widget to QStackedWidget "pages"



  • Ok, so I have a QStackedWidget with layout1 and layout2 as child "pages" when a Qpushbutton is clicked I want to add a widget to layout2 (that plays a video).

    So, in my buttonclicked() I have:
    QMediaPlayer *player = new QMediaPlayer;
    QVideoWidget *vw = new QVideoWidget;
    player->setVideoOutput(vw);
    player->setVolume(50);
    player->setMedia(QUrl::fromLocalFile("/home/pi/Videos/testvideo.wmv"));
    vw->show();
    player->play();

    This works and plays the video, but it opens a new "window" when I call vw->show(); instead of that I want to put the QVideoWidget "vw" on my child page "layer2" of my QStackedWidget "stackedWidget".

    I tried layer2->addWidget(vw); but obviously that was wrong, lol...


  • Lifetime Qt Champion

    @MichRX7 said in Add Widget to QStackedWidget "pages":

    I tried layer2->addWidget(vw); but obviously that was wrong, lol...

    Why is this wrong?



  • @Christian-Ehrlicher said in Add Widget to QStackedWidget "pages":

    @MichRX7 said in Add Widget to QStackedWidget "pages":

    I tried layer2->addWidget(vw); but obviously that was wrong, lol...

    Why is this wrong?

    Well, when I add that line of code it tells me: "Use of undeclared identifier 'layer2'" and won't compile, so I assumed it was quite wrong.


  • Lifetime Qt Champion

    You have to store it as member if you want to access it later in another function in your class - c++ basics...



  • I understand the basics, but being new to QT I didn't understand why I could set aspects of a QLabel on my layer1 in that same function, but not this on my layer2? If I "drew" the QStackedWidget and the layer1/layer2 pages in the UI how can I set that layer2 so that it is accessible in my button click function?

    Thank you for your input.


  • Lifetime Qt Champion

    @MichRX7 said in Add Widget to QStackedWidget "pages":

    I understand the basics,

    I doubt so - storing some objects and pointers in a class and adding getter/accessors to them so they're accessible from the outside has nothing to do with Qt. It's plain object oriented c++.

    What Qt can do for you here is that you can emit a signal when the QPushButton is clicked which you connect to a slot on the second tab. But that's more or less the same than simply using functions, maybe a little bit easier.


  • Lifetime Qt Champion

    @MichRX7 said in Add Widget to QStackedWidget "pages":

    Well, when I add that line of code it tells me: "Use of undeclared identifier 'layer2'" and won't compile, so I assumed it was quite wrong.

    Just as a note. the code you show says "layout2" and not "layer"
    so did you use the right name when added it to the layout ?

    layer2->addWidget(vw); --> layout2->addWidget(vw);

    It is the right way.

    The reason they come windows, when you call show() is that they have no parent
    QVideoWidget *vw = new QVideoWidget; ( no parent assigned )
    So adding them to the layout would solve that.


Log in to reply