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. I can't change pages from a Qwidget to a Qmainwindow
Forum Updated to NodeBB v4.3 + New Features

I can't change pages from a Qwidget to a Qmainwindow

Scheduled Pinned Locked Moved General and Desktop
16 Posts 5 Posters 1.6k Views 3 Watching
  • 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.
  • Mahin AbidM Offline
    Mahin AbidM Offline
    Mahin Abid
    wrote on last edited by
    #1

    I am building a application that have some pages. I want to use Main window so that i can get the toolbar. Let's say i have a page 1 which is a widget now using a push button i want to go to the main window but it seems i cannot access the main window when i click the push button the main window doesn't appear.
    I have tried modal approach and modal less approach
    Can someone help me?
    I am new to qt so it will be helpful if you can describe me fully...

    JonBJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Please be more descriptive about what your application should do.

      Usually when someone is talking about pages it makes sense to explore QWizard however if you are not building a wizard, you likely will be interested in QStackedWidget rather than trying to go back and forth with several instances of QMainWindow.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      Mahin AbidM 1 Reply Last reply
      1
      • Mahin AbidM Mahin Abid

        I am building a application that have some pages. I want to use Main window so that i can get the toolbar. Let's say i have a page 1 which is a widget now using a push button i want to go to the main window but it seems i cannot access the main window when i click the push button the main window doesn't appear.
        I have tried modal approach and modal less approach
        Can someone help me?
        I am new to qt so it will be helpful if you can describe me fully...

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @Mahin-Abid
        You would have to show your code, there is nothing special about QMainWindow so you should be able to do whatever you are trying to achieve. There is no "modal/modeless" in this case, that only applies to QDialogs, and QMainWindow is not one of those.

        Meanwhile, I always wonder when people start talking about showing/hiding the main window. It is more usual that the main window would never be hidden once shown initially. If you have "pages" you wish to display, one possibility is to use a QStackedWidget as a widget inside the main window, that can show any one of a number of different widgets at any one time, That may or may not fit in with what you are looking for.

        Mahin AbidM 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi,

          Please be more descriptive about what your application should do.

          Usually when someone is talking about pages it makes sense to explore QWizard however if you are not building a wizard, you likely will be interested in QStackedWidget rather than trying to go back and forth with several instances of QMainWindow.

          Mahin AbidM Offline
          Mahin AbidM Offline
          Mahin Abid
          wrote on last edited by
          #4

          @SGaist Hi, I want to make a diary app that have a sign in sign up page and then a page where the user can write their diary. As i need mainwindow for menu bar and toolbar so i tried to use stacked widget but then the problem was the menu bar and toolbar is shown in every pages. I need them to show just in one page. It will be also helpful if you can help me by telling me how to add a menu bar or a tool bar in a widget which is not a mainwindow.

          1 Reply Last reply
          0
          • JonBJ JonB

            @Mahin-Abid
            You would have to show your code, there is nothing special about QMainWindow so you should be able to do whatever you are trying to achieve. There is no "modal/modeless" in this case, that only applies to QDialogs, and QMainWindow is not one of those.

            Meanwhile, I always wonder when people start talking about showing/hiding the main window. It is more usual that the main window would never be hidden once shown initially. If you have "pages" you wish to display, one possibility is to use a QStackedWidget as a widget inside the main window, that can show any one of a number of different widgets at any one time, That may or may not fit in with what you are looking for.

            Mahin AbidM Offline
            Mahin AbidM Offline
            Mahin Abid
            wrote on last edited by
            #5

            @JonB Hi, Sorry that i didn't said it right at first. it was not a widget i was using QDialog .I want to make a diary app that have a sign in sign up page and then a page where the user can write their diary. As i need mainwindow for menu bar and toolbar so i tried to use stacked widget in the mainwindow and then add pages for sign in and sign ups.But then the problem was the menu bar and toolbar is shown in every pages. I need them to show just in one page. It will be also helpful if you can help me by telling me how to add a menu bar or a tool bar in a widget or in a Qdialog which is not a mainwindow.

            JonBJ 1 Reply Last reply
            0
            • Mahin AbidM Mahin Abid

              @JonB Hi, Sorry that i didn't said it right at first. it was not a widget i was using QDialog .I want to make a diary app that have a sign in sign up page and then a page where the user can write their diary. As i need mainwindow for menu bar and toolbar so i tried to use stacked widget in the mainwindow and then add pages for sign in and sign ups.But then the problem was the menu bar and toolbar is shown in every pages. I need them to show just in one page. It will be also helpful if you can help me by telling me how to add a menu bar or a tool bar in a widget or in a Qdialog which is not a mainwindow.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @Mahin-Abid
              Then it sounds for the logon like you want a modal QDialog to be exec()ed from your main() before you go on to display the QMainWindow. Do that rather than trying to put the logon on the QMainWindow/in a QStackedWidget.

              W Mahin AbidM 2 Replies Last reply
              1
              • JonBJ JonB

                @Mahin-Abid
                Then it sounds for the logon like you want a modal QDialog to be exec()ed from your main() before you go on to display the QMainWindow. Do that rather than trying to put the logon on the QMainWindow/in a QStackedWidget.

                W Offline
                W Offline
                Widdershins_NH
                wrote on last edited by
                #7

                @JonB hi, I have similar problem with my application. my problem is i am completely new to this so i can't even undersatnd your solution. it will be very helpful if you can demonstrate it or can give me solution like what should i code and what should i code.

                1 Reply Last reply
                0
                • JonBJ JonB

                  @Mahin-Abid
                  Then it sounds for the logon like you want a modal QDialog to be exec()ed from your main() before you go on to display the QMainWindow. Do that rather than trying to put the logon on the QMainWindow/in a QStackedWidget.

                  Mahin AbidM Offline
                  Mahin AbidM Offline
                  Mahin Abid
                  wrote on last edited by
                  #8

                  @JonB do you mean
                  MyDialog MyDialog;
                  MyDialog.setModal(true);
                  MyDialog.exec();
                  this code?

                  mrjjM 1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Yes, that's what @JonB is suggesting. You'll have to then check the result of the result of the login process before continuing.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    Mahin AbidM 2 Replies Last reply
                    2
                    • Mahin AbidM Mahin Abid

                      @JonB do you mean
                      MyDialog MyDialog;
                      MyDialog.setModal(true);
                      MyDialog.exec();
                      this code?

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

                      @Mahin-Abid
                      yes in main.cpp before showing Mainwindow.
                      the exec() is a so called blocking function so it will stay in there until user press close or ok. Your Dialog should validate the credentials.

                      int main(int argc, char *argv[])
                      {
                          QApplication a(argc, argv);
                          MainWindow w;
                         MyDialog Dialog;  
                       if ( Dialog.exec() ==  QDialog::Accepted)
                          w.show();
                      else
                        QMessageBox::warning(this, tr("My Application"),tr("invalid login.\n");
                      
                          ... ask again or quit the app-                              
                          return a.exec();
                      }
                      
                      
                      1 Reply Last reply
                      1
                      • SGaistS SGaist

                        Yes, that's what @JonB is suggesting. You'll have to then check the result of the result of the login process before continuing.

                        Mahin AbidM Offline
                        Mahin AbidM Offline
                        Mahin Abid
                        wrote on last edited by
                        #11

                        @SGaist problem is it's showing there is no membar called setModal

                        mrjjM JonBJ 2 Replies Last reply
                        0
                        • Mahin AbidM Mahin Abid

                          @SGaist problem is it's showing there is no membar called setModal

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

                          @Mahin-Abid
                          But is MyDialog based on QDialog class ?

                          1 Reply Last reply
                          0
                          • SGaistS SGaist

                            Yes, that's what @JonB is suggesting. You'll have to then check the result of the result of the login process before continuing.

                            Mahin AbidM Offline
                            Mahin AbidM Offline
                            Mahin Abid
                            wrote on last edited by
                            #13

                            @SGaist If i am not clear i want to go to a mainwindow from a Qdialog using a push button . I tried went to the slot of the push button and write the code but it's saying there is no member called setModal. I also tried by creating pointer Which is modal approach in header file of the dialog then the problem was the mainwindow pops for 1 sec and the whole application gets terminated.

                            mrjjM 1 Reply Last reply
                            0
                            • Mahin AbidM Mahin Abid

                              @SGaist If i am not clear i want to go to a mainwindow from a Qdialog using a push button . I tried went to the slot of the push button and write the code but it's saying there is no member called setModal. I also tried by creating pointer Which is modal approach in header file of the dialog then the problem was the mainwindow pops for 1 sec and the whole application gets terminated.

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

                              @Mahin-Abid
                              well without the actual code, hard to guess what went wrong.
                              But is the goal not to pop the winow before showing Dialog to get login and if ok, then
                              show MainWindow ?

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

                                Hi
                                Here is sample of login / then show mainwindow.
                                In main.cpp it opens LoginDialog and ask for password.
                                iF password is 1234 then show main window, else show message and quit application.

                                https://www.dropbox.com/s/ricjl8t9ocjmd27/LoginTest.zip?dl=0

                                1 Reply Last reply
                                2
                                • Mahin AbidM Mahin Abid

                                  @SGaist problem is it's showing there is no membar called setModal

                                  JonBJ Offline
                                  JonBJ Offline
                                  JonB
                                  wrote on last edited by
                                  #16

                                  @Mahin-Abid said in I can't change pages from a Qwidget to a Qmainwindow:

                                  @SGaist problem is it's showing there is no membar called setModal

                                  That's because only a QDialog --- or something derived from it --- can be shown modally. "Modal" means it displays itself and then waits until the user does something on it to cause it to close. You can only do that on a "dialog". QMainWindow does not derive from QDialog, it just a "plain" window. It is not the main window you wish to be modal, it is the dialog asking for the username/password.

                                  Take a breath and look at @mrjj's sample code above, I've put in a couple of comments:

                                  int main(int argc, char *argv[])
                                  {
                                      # Create a `QApplication`
                                      # nothing visual happens at this point
                                      # but this is required in order to make Qt calls
                                      QApplication a(argc, argv);
                                      # Declare the main window
                                      # Again, nothing visual happens here yet
                                      MainWindow w;
                                      # and similarly declare a dialog
                                      MyDialog Dialog;  
                                      # Display the dialog, and wait for the user to interact with it (fill in name/password), and click `OK`
                                      # This all happens because we call `QDialog::exec()`
                                      # You don't even need to call `QDialog::setModal(true)` because `exec()` does that automatically for you
                                      # Have the dialog code check the username + password
                                      # If they are good have it call `QDialog::accept()`, which returns `QDialog::Accepted` here
                                      # else if they are bad have it call  `QDialog::reject()`
                                      if ( Dialog.exec() ==  QDialog::Accepted)
                                         # Only now do we finally show the `QMainWindow`
                                         # we simply `show()` it, nothing "modal" here
                                         w.show();
                                      else
                                         QMessageBox::warning(this, tr("My Application"),tr("invalid login.\n");
                                  
                                      ... ask again or quit the app-      
                                      # we must call `QApplication::exec()` to have the Qt event loop running
                                      # note that this `exec()` has nothing to do with `QDialog::exec()` used earlier                    
                                      return a.exec();
                                  }
                                  
                                  1 Reply Last reply
                                  1

                                  • Login

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