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