PDA

View Full Version : How do I back to FirstWindow from SecondWindow



Tuyen Trong Tran
29th March 2017, 17:38
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[/QTCLASS]

SecondWindow.h


#ifndef SECONDWINDOW_H
#define SECONDWINDOW_H
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();
}

d_stranz
29th March 2017, 17:57
Please use
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:
[CODE]
void FirstWindow::on_pushButton_clicked()
{
hide();
secondwindow = new SecondWindow(this);
secondwindow->show();
}

to this:



void FirstWindow::on_pushButton_clicked()
{
hide();
SecondWindow secondWindow(this);
secondwindow.exec();
show();
}

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:



void SecondWindow::on_pushButton_clicked()
{
hide();
firstwindow=new FirstWindow(this);
firstwindow->show();
}

to this:



void SecondWindow::on_pushButton_clicked()
{
accept();
}

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 -> ...