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

How to do transition from 2nd page to 1st page



  • We have design Two Forms (MainWindow.ui & newwindow.ui) we are Sucessfully done designing of Transiting from 1st Form to 2nd form But we are not able to Transit from 2nd page to 1 st Page. below is the Program for Transition of 1st page to 2nd page. We are not able to do transition from 2nd page to 1st page

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMainWindow>

    //added
    #include"newwindow.h"

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    //added
    public slots:
       void openNewWindow();
    
    //added name of the new window is NewWindow
    private:
       NewWindow *mMyNewWindow;
    
    private:
        Ui::MainWindow *ui;
    
    private slots:
        void on_mMyButton_clicked();
    

    };
    #endif // MAINWINDOW_H

    My newwindow.h is as follows.

    #ifndef NEWWINDOW_H
    #define NEWWINDOW_H

    #include <QMainWindow>

    namespace Ui {
    class NewWindow;
    }

    class NewWindow : public QMainWindow
    {
    Q_OBJECT

    public:
        explicit NewWindow(QWidget *parent = 0);
        ~NewWindow();
    
    private:
        Ui::NewWindow *ui;
    

    };

    #endif // NEWWINDOW_H

    My mainwindow.cpp is as follows.

    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    //Added
    connect(ui->mMyButton, SIGNAL(click()), this, SLOT(openNewWindow()));
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    My newwindow.cpp,

    #include "newwindow.h"
    #include "ui_newwindow.h"

    NewWindow::NewWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::NewWindow)
    {
    ui->setupUi(this);
    }

    NewWindow::~NewWindow()
    {
    delete ui;
    }

    My main.cpp as,

    #include <QtGui/QApplication>
    #include "mainwindow.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec&#40;&#41;;
    

    }

    Thanks for all the information. And enjoy the programming with Qt.

    //In my mainwindow.cpp also add the following lines.

    void MainWindow::openNewWindow()
    {
    mMyNewWindow = new NewWindow();

    mMyNewWindow->show();
    

    }

    void MainWindow::on_mMyButton_clicked()
    {
    openNewWindow()

    }

    Dharmendra



  • Easiest way to do what you need is to create one main window change it's layout using a stacked layout and voila :) and u can switch between how many pages you want...



  • StackLayout sounds also good to me, but please use code tags for your snippets (put it between at signs).


Log in to reply