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. Show mainwindow from another window
QtWS25 Last Chance

Show mainwindow from another window

Scheduled Pinned Locked Moved Solved General and Desktop
designermainwindowc++
8 Posts 3 Posters 9.7k Views
  • 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.
  • ronyNSR Offline
    ronyNSR Offline
    ronyNS
    wrote on last edited by
    #1

    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

    this->hide();
            emp=new employeeinfo();
            emp->show();
    

    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.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      so maybe do
      this->hide();
      emp=new employeeinfo(this);
      emp->show();

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

      ronyNSR 1 Reply Last reply
      2
      • R Offline
        R Offline
        Rondog
        wrote on last edited by
        #3

        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
        {
        Q_OBJECT
        public:
        	MyOtherWindow(QWidget *parent=0, Qt::WindowFlags flags=0);
        	~MyOtherWindow(void);
        
        private:
        	MainWindow *main_window; // forward declaration required otherwise this is an error
        };
        
        #endif
        
        1 Reply Last reply
        1
        • mrjjM mrjj

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

          so maybe do
          this->hide();
          emp=new employeeinfo(this);
          emp->show();

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

          ronyNSR Offline
          ronyNSR Offline
          ronyNS
          wrote on last edited by
          #4

          @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 :)

          mrjjM 1 Reply Last reply
          0
          • ronyNSR ronyNS

            @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 :)

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

            @ronyNS

            Hi
            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()
            newmain->show()

            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 :)

            1 Reply Last reply
            1
            • ronyNSR Offline
              ronyNSR Offline
              ronyNS
              wrote on last edited by
              #6

              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?

              mrjjM 1 Reply Last reply
              0
              • ronyNSR ronyNS

                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?

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

                @ronyNS
                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 ) {
                d2.exec();
                } 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

                etc.

                ronyNSR 1 Reply Last reply
                2
                • mrjjM mrjj

                  @ronyNS
                  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 ) {
                  d2.exec();
                  } 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

                  etc.

                  ronyNSR Offline
                  ronyNSR Offline
                  ronyNS
                  wrote on last edited by
                  #8

                  @mrjj
                  Got it , Thanks :)

                  1 Reply Last reply
                  0

                  • Login

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