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.
  • R Offline
    R Offline
    ronyNS
    wrote on 16 Aug 2016, 18:07 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
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 16 Aug 2016, 18:44 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.

      R 1 Reply Last reply 17 Aug 2016, 07:27
      2
      • R Offline
        R Offline
        Rondog
        wrote on 16 Aug 2016, 18:48 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
        • M mrjj
          16 Aug 2016, 18:44

          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.

          R Offline
          R Offline
          ronyNS
          wrote on 17 Aug 2016, 07:27 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 :)

          M 1 Reply Last reply 17 Aug 2016, 07:38
          0
          • R ronyNS
            17 Aug 2016, 07:27

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

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 17 Aug 2016, 07:38 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
            • R Offline
              R Offline
              ronyNS
              wrote on 17 Aug 2016, 07:46 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?

              M 1 Reply Last reply 17 Aug 2016, 07:55
              0
              • R ronyNS
                17 Aug 2016, 07:46

                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?

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 17 Aug 2016, 07:55 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.

                R 1 Reply Last reply 17 Aug 2016, 07:59
                2
                • M mrjj
                  17 Aug 2016, 07:55

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

                  R Offline
                  R Offline
                  ronyNS
                  wrote on 17 Aug 2016, 07:59 last edited by
                  #8

                  @mrjj
                  Got it , Thanks :)

                  1 Reply Last reply
                  0

                  8/8

                  17 Aug 2016, 07:59

                  • Login

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