Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How do I back to FirstWindow from SecondWindow
Qt 6.11 is out! See what's new in the release blog

How do I back to FirstWindow from SecondWindow

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.9k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TuyenTT
    wrote on last edited by
    #1

    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();
    }
    
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      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.

      T 1 Reply Last reply
      1
      • mrjjM mrjj

        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.

        T Offline
        T Offline
        TuyenTT
        wrote on last edited by
        #3

        @mrjj but I'm need to hide Main Window

        mrjjM 1 Reply Last reply
        0
        • T TuyenTT

          @mrjj but I'm need to hide Main Window

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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?

          T 1 Reply Last reply
          4
          • mrjjM mrjj

            @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?

            T Offline
            T Offline
            TuyenTT
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi and welcome to devnet,

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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved