allin0n3 allin0n3 - 3 months ago 7
C++ Question

Qt: button - going back from "help.cpp" to "mainwindow.cpp"

I'm new at Qt. I've created small application and I created second page

help.cpp
. On
MainWindow.cpp
I have a button, that switches to
help.cpp
page.

Function which switches to "help" page:

void MainWindow::on_box1button_clicked()
{
helpwindow = new help(this);
helpwindow->show();
}


This code works properly.

On the "help" page I've got a
QButton
, which will switch back to
mainwindow.cpp
. How Can I code that button to actually make this action?

Answer

If your intention by "switching" is hiding one window and showing another one, so you can simply pass a reference of the main window to your help window and there when you want to switch back, you can hide/close itself and show the main window.

MainWindow (this code is fine)

helpwindow = new help(this);
helpwindow->show();

HelpWindow

When you want to switch back to the main window, you can do this:

// Hide the HelpWindow itself
// or this->close()
this->hide() 

// Show the MainWindow (i.e. the parent window)
QWidget *parent = parentWidget();
parent->show();