When clicking a button, how to make window transition to another window without opening a new window.



  • I know how to create a new window when clicking a button. However, I would like it so that when you click on a button ( for example, "Next" or "Ok") on a window, it transitions into the same window but with a different layout without creating a new window. I will be creating approximately 10 transitions which is why I don't want a lot of windows to be opening. I have been looking in a lot of forums but I can't seem to find anything. I am a beginner so I am not sure where to start.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Looks like you want QStackedWidget or QStackedLayout



  • @SGaist I was looking at their description and it states that it: "provides a stack of widgets where only one widget is visible at a time." However, I would like it so that multiple widgets are visible at a time. For example, window #1 will have like a sign in : text box, label, and a button. On window #2, it will have student input information. All the widgets will change once it has been transitioned into another window.


  • Lifetime Qt Champion

    Take a look at the linked Config Dialog Example

    You'll see that one widget doesn't mean it can't be composed



  • I am a beginner. Many of the things shown in the example I don't fully understand. Do you think you can show me a much simpler example if possible? I would really appreciate it.



  • Also, I just ran the program from the example you provided me link and it is not exactly what I am looking for. When I ran this program, it looks like it has three tabs and whenever you click on one of the tabs, it will go to a different window. I would like it so that when I click on the "Ok" button, it goes to the next window without opening a new one. I know it is too much of ask but I am lost.


  • Lifetime Qt Champion

    This is a quick and dirty example from the top of my head. It could have been made simpler by using a lambda but so you'll see that QStackedWidget is working the way you want it.

    class MyDialog : public QDialog
    {
    Q_OBJECT
    
    public:
    explicit MyDialog(QWidget *parent=0);
    
    private slots:
    void gotToPage2();
    
    private:
    QStackedWidget *_stackedWidget;
    };
    
    MyDialog::MyDialog(QWidget *parent)
       : QDialog(parent)
    {
    
    // Create first widget
    QWidget *firstPage = new QWidget;
    QLabel *instructionLabel = new QLabel(tr("Instruction for user"));
    QLineEdit *userNameLineEdit = new QLineEdit;
    QPushButton *page2PushButton = new QPushButton(tr("Page 2"));
    QVBoxLayout *firstPageLayout = new QVBoxLayout(firstPage); // automatically set the layout on the widget
    firstPageLayout->addWidget(instructionLabel);
    firstPageLayout->addWidget(userNameLineEdit);
    firstPageLayout->addWidget(page2PushButton);
    
    // Create second widget
    QWidget *secondPage = new QWIdget;
    QLineEdit *firstNameLineEdit = new QLineEdit;
    QLineEdit *lastNameLineEdit = new QLineEdit;
    QPushButton *endPushButton = new QPushButton(tr("End"));
    QVBoxLayout *secondPageLayout = new QVBoxLayout(secondPage);
    secondPageLayout->addWidget(firstName);
    secondPageLayout->addWidget(lastNameLineEdit);
    secondPageLayout->addWidget(endPushButton);
    
    _stackedWidget = new QStackedWidget;
    stackedWidget->addWidget(firstPage);
    stackedWidget->addWidget(secondPage);
    
    connect(page2PushButton, &QPushButton::clicked, this, &MyDialog::goToPage2);
    }
    
    void MyDialog::gotToPage2()
    {
       stackedWidget->setCurrentIndex(1);
    }
    

    Note that you could also be interested by QWizard


Log in to reply
 

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