Results 1 to 2 of 2

Thread: How do I back to FirstWindow from SecondWindow

  1. #1
    Join Date
    Mar 2017
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default 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
    Qt Code:
    1. #ifndef FIRSTWINDOW_H
    2. #define FIRSTWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <secondwindow.h>
    6.  
    7. namespace Ui {
    8. class FirstWindow;
    9. }
    10.  
    11. class FirstWindow : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit FirstWindow(QWidget *parent = 0);
    17. ~FirstWindow();
    18.  
    19. private slots:
    20. void on_pushButton_clicked();
    21.  
    22. private:
    23. Ui::FirstWindow *ui;
    24. SecondWindow *secondwindow;
    25. };
    26.  
    27. #endif // FIRSTWINDOW_H[/QTCLASS]
    To copy to clipboard, switch view to plain text mode 
    SecondWindow.h
    Qt Code:
    1. #ifndef SECONDWINDOW_H
    2. #define SECONDWINDOW_H
    3. SecondWindow.h
    4. #include <QDialog>
    5. #include <firstwindow.h>
    6. namespace Ui {
    7. class SecondWindow;
    8. }
    9.  
    10. class SecondWindow : public QDialog
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit SecondWindow(QWidget *parent = 0);
    16. ~SecondWindow();
    17.  
    18. private slots:
    19. void on_pushButton_clicked();
    20.  
    21. private:
    22. Ui::SecondWindow *ui;
    23. FirstWindow *firstwindow;
    24.  
    25. };
    26.  
    27. #endif // SECONDWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    FirstWindow.cpp
    Qt Code:
    1. #include "firstwindow.h"
    2. #include "ui_firstwindow.h"
    3.  
    4. FirstWindow::FirstWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::FirstWindow)
    7. {
    8. ui->setupUi(this);
    9. setHidden(true);
    10. }
    11.  
    12. FirstWindow::~FirstWindow()
    13. {
    14. delete ui;
    15. }
    16.  
    17. void FirstWindow::on_pushButton_clicked()
    18. {
    19. hide();
    20. secondwindow = new SecondWindow(this);
    21. secondwindow->show();
    22. }
    To copy to clipboard, switch view to plain text mode 
    SecondWindow.cpp
    Qt Code:
    1. #include "secondwindow.h"
    2. #include "ui_secondwindow.h"
    3.  
    4. SecondWindow::SecondWindow(QWidget *parent) :
    5. QDialog(parent),
    6. ui(new Ui::SecondWindow)
    7. {
    8. ui->setupUi(this);
    9.  
    10. }
    11.  
    12. SecondWindow::~SecondWindow()
    13. {
    14. delete ui;
    15. }
    16.  
    17.  
    18.  
    19. void SecondWindow::on_pushButton_clicked()
    20. {
    21. hide();
    22. firstwindow=new FirstWindow(this);
    23. firstwindow->show();
    24. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by high_flyer; 2nd April 2017 at 20:08.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How do I back to FirstWindow from SecondWindow

    Please use [CODE] tags when posting source code. See my signature block below.

    In your FirstWindow constructor, you hide it immediately. How can you click the push button if the window is hidden?

    Change this function:
    Qt Code:
    1. void FirstWindow::on_pushButton_clicked()
    2. {
    3. hide();
    4. secondwindow = new SecondWindow(this);
    5. secondwindow->show();
    6. }
    To copy to clipboard, switch view to plain text mode 

    to this:

    Qt Code:
    1. void FirstWindow::on_pushButton_clicked()
    2. {
    3. hide();
    4. SecondWindow secondWindow(this);
    5. secondwindow.exec();
    6. show();
    7. }
    To copy to clipboard, switch view to plain text mode 

    This does two things: it changes the instance of "SecondWindow" (which is a QDialog) so it is allocated on the stack instead of on the heap, and also executes it as a modal dialog. Execution of the FirstWindow:: on_pushButton_clicked() is blocked until the exec() call returns. When it does return, the FirstWindow instance is shown again.

    Next, change this:

    Qt Code:
    1. void SecondWindow::on_pushButton_clicked()
    2. {
    3. hide();
    4. firstwindow=new FirstWindow(this);
    5. firstwindow->show();
    6. }
    To copy to clipboard, switch view to plain text mode 

    to this:

    Qt Code:
    1. void SecondWindow::on_pushButton_clicked()
    2. {
    3. accept();
    4. }
    To copy to clipboard, switch view to plain text mode 

    This closes the dialog and returns to the FirstWindow slot that called exec() on the SecondWindow instance.

    Your original code for this method was very wrong, because it would have resulted in two instances of FirstWindow - the original one that created the first SecondWindow instance, and a new one that was created as a child of that instance. If you kept clicking buttons, you would eventually end up with multiple instances of both FirstWindow and SecondWindow, with parent-child relationships like this:

    FirstWindow0 -> SecondWindow0 -> FirstWindow1 -> SecondWindow1 -> FirstWindow2 -> SecondWindow2 -> ...
    Last edited by d_stranz; 29th March 2017 at 18:11.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. QML to Qt and back
    By forty-two in forum Qt Quick
    Replies: 3
    Last Post: 1st July 2012, 21:59
  2. QSharedPointer in a QVariant and back again
    By tomschuring in forum Qt Programming
    Replies: 2
    Last Post: 24th January 2012, 23:02
  3. can not go back to the previous window
    By xhsoldier in forum Qt Programming
    Replies: 1
    Last Post: 20th October 2010, 05:45
  4. Back tracing asserts with gdb
    By ucomesdag in forum Qt Programming
    Replies: 8
    Last Post: 30th July 2007, 21:59
  5. drop image fly back
    By sreedhar in forum Qt Programming
    Replies: 5
    Last Post: 8th March 2006, 12:00

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.