stackedwidget for different class which they have their own .cpp , .h, and .ui

  • I have created different classes(.cpp, .h, .ui) and I wish to combine all of their .ui's in a stackedwidget. I created an extra class specifically to add the widgets into the stackedwidget. For the stackedwidget cpp I have used

    QStackedWidget *stackedWidget = new QStackedWidget;

    page1 would be the widget I create for one cpp/ui file and on the stackedwidget cpp I made sure to include the .h file for page_1. On the page_1 .cpp (I made it into QDialog) and wrote the following:

    QWidget *page1 = new QWidget;
    QVBoxLayout *finalLayout = new QVBoxLayout(page1);

    However, the stackedwidget cpp does not recognize the QWidget page1.

  • Lifetime Qt Champion


    Can you show the complete code where you setup your QStackedWidget ? Where do you declare/instantiate page1 ?

  • Page_1.cpp has

    QWidget *page1 = new QWidget;
    ui->page1Layout = new QVBoxLayout(page1);

    Page_stackedWidget.cpp has the '#include Page_1.h'

    stackedWidget = new QStackedWidget;
    QVBoxLayout *vLayout = new QVBoxLayout;

  • Lifetime Qt Champion

    page1 is a local variable used in Page_1.cpp, you can't access it like that in Page_stackedWidget.cpp (and don't try or you are going to write pretty ugly code).

    Shouldn't you be doing something like:

    Page_1 *page_1 = new Page_1;


  • The thing is there were going to be multiple pages and I wanted to stack the widgets in a different file. I was able to achieve that with Page_stackedWidget.ui. However, I would like it so when I press a button that is in page 1 it takes me to page 2. This is what I have for Page_stackedWidget.cpp

    #include "dialog.h"
    #include "ui_dialog.h"
    Page_stackedWidget::Page_stackedWidget(QWidget *parent) :
    ui(new Ui::Dialog)
    connect(_signin->nextButton, SIGNAL(clicked()),this,SLOT(goToPage2())); 
    void Dialog::goToPage2()

    However, the "nextButton" I created on the .ui file for Page1. I have looked for forums and i have found some similar to my problem but the difference is that their button is created on this file.

  • Qt Champions 2016

    If I understand you correctly,
    you can maybe use the promote feature.

    Say you have
    UI A,B,C
    B and C contains widgets like buttons etc.

    on A you put a stacked widget
    and on page 1 it has all of B, and on page 2, it has all of C
    and they still live in their own UI files. ?

    That you can do via promote. In short you place a QWidget in the stacked paged and it will be replaced
    by all widget from an UI file. When you run the program.
    Note: select QWidget as parent, not dialog when you create B,C.

    Please see

    If its not what you mean, then sorry :)

  • @mrjj I did put a widget on the stackedWidget. Even though it is a QDialog, I created a widget inside of it. I was able to call the files and add them into the stackwidget. My question now is how do I call a qpushbutton created from Page1?

  • Qt Champions 2016

    well , say the place holder widget is called Wid2, (the one you promoted)
    then you can access any widget using the normal


    its being created in the ui_xx file for the UI that contains the promoted widgets.

  • Lifetime Qt Champion

    Each widget should be self-contained so what exactly do you want to do with that button from another widget in your stack ?

  • I want to press that button to go the next page that is in the stack

  • Lifetime Qt Champion

    In that case and from a design point of view, does it really belong to a widget inside the stack ?

  • Yes it does.

  • Lifetime Qt Champion

    Then add a signal to that widget that will forward the button clicked signal. Something like nextPageRequested. That will be cleaner and easier to understand when you read the code.

  • I know but how am I supposed to access that widget when it is in another class?

    connect(Page1->nextButton, SIGNAL(clicked()),this,SLOT(goToPage2()));

    Where Page1 is the widget that contains the button "nextButton" and when you press it will go to Page2

  • Lifetime Qt Champion

    That's what I wrote: you don't access the button, you only connect Page1.

    Are you trying to re-implement QWizard ?

  • Yes but I do not want to use QWizard since I don't want it in that style. The only similarity is that I can go from one page to another.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.