How do I back to FirstWindow from SecondWindow



  • I am a QT beginner. I am want to back FirstWindow from SecondWindow. I could open SecondWindow from FirstWindow, but doing the opposite does not work . Can you help me!
    FirstWindow.h

    #ifndef FIRSTWINDOW_H
    #define FIRSTWINDOW_H
    
    #include <QMainWindow>
    #include <secondwindow.h>
    
    namespace Ui {
    class FirstWindow;
    }
    
    class FirstWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit FirstWindow(QWidget *parent = 0);
        ~FirstWindow();
    
    private slots:
        void on_pushButton_clicked();
    
    private:
        Ui::FirstWindow *ui;
        SecondWindow *secondwindow;
    };
    
    #endif // FIRSTWINDOW_H
    
    

    SecondWindow.h

    #ifndef SECONDWINDOW_H
    #define SECONDWINDOW_H
    
    #include <QDialog>
    #include <firstwindow.h>
    namespace Ui {
    class SecondWindow;
    }
    
    class SecondWindow : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit SecondWindow(QWidget *parent = 0);
        ~SecondWindow();
    
    private slots:
        void on_pushButton_clicked();
    
    private:
        Ui::SecondWindow *ui;
        FirstWindow *firstwindow;
    
    };
    
    #endif // SECONDWINDOW_H
    
    

    FirstWindow.cpp

    #include "firstwindow.h"
    #include "ui_firstwindow.h"
    
    FirstWindow::FirstWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::FirstWindow)
    {
        ui->setupUi(this);
        setHidden(true);
    }
    
    FirstWindow::~FirstWindow()
    {
        delete ui;
    }
    
    void FirstWindow::on_pushButton_clicked()
    {
        hide();
        secondwindow = new SecondWindow(this);
        secondwindow->show();
    }
    
    

    SecondWindow.cpp

    #include "secondwindow.h"
    #include "ui_secondwindow.h"
    
    SecondWindow::SecondWindow(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::SecondWindow)
    {
        ui->setupUi(this);
    
    }
    
    SecondWindow::~SecondWindow()
    {
        delete ui;
    }
    
    
    
    void SecondWindow::on_pushButton_clicked()
    {
    hide();
    firstwindow=new FirstWindow(this);
    firstwindow->show();
    }
    
    

  • Qt Champions 2016

    Hi
    First window being a mainwindow
    If you want to pop up a Dialog ( second window) and then go back to main window when
    Dialog is closed, why not just do

    void FirstWindow::on_pushButton_clicked()
    {
    SecondWindow dialog(this);
    dialog.exec();
    }

    Then it opens over the mainwindow and u can just close it to show the main window again.



  • @mrjj but I'm need to hide Main Window


  • Qt Champions 2016

    @TuyenTT
    If you need a design where user goes from window to window, often

    http://doc.qt.io/qt-5/qstackedwidget.html
    is much better.

    If you insists on doing it with windows, you need to give window 2 a way to unhide window 1
    ( you create a new each time, which is not what u want)

    For that you can use a pointer or signals.

    Mostly depends on what else you need
    You want to use more than 2 windows ?
    Or what is the overall design wish?



  • @mrjj I can not code from scratch. Can you give me the quickest solution.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    To provide you with more information you first have to explain exactly what your application is supposed to do.


Log in to reply
 

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