Show mainwindow from another window

  • Hello
    I want to show the main window from another window.
    For instance
    i have a login form and i managed to open another window from the login form using

            emp=new employeeinfo();

    Now i have a pushbutton on the employeeinfo ui and i want to go back to the main login window when i press the push button.
    i don't know what to include.
    i tried to include "mainwindow.h" and then declare mainwindow *main; in private
    However , it says 'MainWindow' does not name a type.

  • Lifetime Qt Champion

    this->hide(); <<< this hide your existing mainwindow?

    so maybe do
    emp=new employeeinfo(this);

    then in EMP
    you can say Parent->Show() and it will be visible again.

  • This is not really clear. The error suggests you need to add a forward declaration of 'MainWindow' in the header of your other window (?).

    #ifndef HEADERFILE
    #define HEADERFILE
    class MainWindow; // forward declaration
    class MyOtherWindow : public QWidget
    	MyOtherWindow(QWidget *parent=0, Qt::WindowFlags flags=0);
    	MainWindow *main_window; // forward declaration required otherwise this is an error

  • @mrjj

    Hello thank you , this worked :)
    However, i have a question regarding this.
    Is this the right way to toggle between windows.
    I mean we hide the window using this->hide(); , so the window is still using the memory , its just hidden. If i make a program with a lot of windows , all of them will just be hidden and not closed.
    So is this the right way , or is there a different way to load another window from mainwindow.
    Thank you :)

  • Lifetime Qt Champion


    well, normally a program have a mainwindow and pop dialogs over it.
    So mainwin is always shown and the dialogs are created/shown/deleted

    There is nothing wrong in hiding the window but it really depends on what you want.

    You can create a new window each time, but then you are not going back to it, you
    just show a new version.
    in any place in the program , you could

    MainWindow *newmain= new MainWindow()

    and have a brand new mainwindow

    but its not so common to do with mainwindow, its more for dialogs or
    other type of windows.

    your design do sound like the normal one where u pop a dialog over mainwindow.
    except you want to hide mainwindow.

    So its not complete crazy :)

  • Ok so if i want to open a profile page after the login page , a dialog should do?
    I tried using dialog , however it does not have the maximize and minimize buttons , that is why is decided to use main window.
    What do you suggest?

  • Lifetime Qt Champion

    hi i think u can use Qt::WindowMaximizeButtonHint; and other
    setWindowFlags flags to add such min/max.

    well im not really sure what you are after but

    say u show mainwindow

    then do

    LoginDialog d1;
    ProfileDialog d2;

    if ( d1.exec() == QDialog::Accepted ) {
    } else
    Login in failed. what ever to happen

    it would first show login (blocking mainwin)
    if user press ok, then show profile. still blocking mainwind


  • @mrjj
    Got it , Thanks :)