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. Switch to another Form
QtWS25 Last Chance

Switch to another Form

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 3 Posters 3.8k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    I have a Loginform for my application. How can i easily switch to the MainWindow after the login was succesfull? I didnt find a solution yet. Please help me.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      The easiest would be something like this:

      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
      
          YourLoginDialog login_dialog;
          if (login_dialog.exec() != QDialog::Accepted)
          {
              //handle failed login here
              return 0;
          }
      
          MainWindow w;
          w.show();
      
          return a.exec();
      }
      
      1 Reply Last reply
      5
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by A Former User
        #3

        How is it possible to return true with a Dialog? Can i do this with a Widget?

        P 1 Reply Last reply
        0
        • ? A Former User

          How is it possible to return true with a Dialog? Can i do this with a Widget?

          P Offline
          P Offline
          panosk
          wrote on last edited by
          #4

          @Fuel said:

          How is it possible to return true with a Dialog? Can i do this with a Widget?

          Why not? :) It makes perfect sense, you have some form of user interaction (thus, a "dialog") and you have the possibility to examine the result of this interaction. QDialog inherits QWidget, but I think the QDialog::DialogCode enum and the related methods are introduced in QDialog. Have a look at the QDialog docs.

          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by Chris Kawa
            #5

            @Fuel said:

            How is it possible to return true with a Dialog? Can i do this with a Widget?

            A dialog does not return anything. It's a class not a function. A class inheriting QDialog has a convenience exec() method starting an event loop. The dialog sets the return code of that loop for you to inspect.

            You can do this with a QWidget, but you'd be re-inventing a wheel:

            YourLoginWidget login_widget;
            login_widget.setWindowModality(Qt::ApplicationModal);
            login_widget.show();
            
            QEventLoop loop;
            connect(&login_widget, &YourLoginWidget::someLoginSucceededSignal, &loop, &QEventLoop::quit);
            loop.exec();
            if (!login_widget.successfullyLogged())
            {
               //handle login fail
               return 0;
            }
            

            Of course you'd have to emit someLoginSucceededSignal from your widget under some condition and implement successfullyLogged to return your bool.
            All in all a QDialog is much better for this as it does all the heavy lifting for you. All you need to do with it is call accept() if the login was successful or reject() otherwise. QDialog will handle the event loop, modality and other stuff.

            1 Reply Last reply
            2
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              Thanks. I take a look at QDialog

              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                I have a Problem with my QDialog. When i click Ok in my QDialog and i check the password, the QDialog always sets the QDialog as accepted. It doesnt matter if the Password is correct or wrong. I normally use a clicked Slot to perform some Actions and if the Password doesnt match, show a QMessageBox. But the MessageBox shows, if the password is wrong, and the QDialog sets automatically accepted.

                I need a disconnect Signal to prevent QDialog to Close automatically after clicking Ok. How can i do this?

                1 Reply Last reply
                0
                • Chris KawaC Offline
                  Chris KawaC Offline
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on last edited by Chris Kawa
                  #8

                  @Fuel said:

                  I need a disconnect Signal to prevent QDialog to Close automatically after clicking Ok. How can i do this?

                  That would be working around a built-in mechanism of the designer (I assume your dialog has a QDialogButtonBox and is connected automatically by the uic). The intended way of doing this is overriding the accept slot and doing your logic there. Something along the lines of this:

                  void YourDialog::accept()
                  {
                      if ( /* password is correct */)
                          QDialog::accept(); 
                      else
                          QMessageBox::warning(this, tr("Login failed"), tr("Bummer. The password is incorrect :("), QMessageBox::Ok);
                  }
                  
                  1 Reply Last reply
                  1
                  • ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by
                    #9

                    Ok that works. but my Dialog closes after he shows the QMessageBox and the User isnt available to re-enter the Password.

                    1 Reply Last reply
                    0
                    • Chris KawaC Offline
                      Chris KawaC Offline
                      Chris Kawa
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      That shouldn't happen as the "closing code" is only in QDialog::accept. Can you show your actual code? Connections, button setup etc.

                      1 Reply Last reply
                      0
                      • ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by A Former User
                        #11

                        this is my method if the button ok is pressed. edit: uhmm thats the dialog before my other one shows. its set a password on first start. but it closes.

                        void PasswordDialog::acceptedSlot()
                        {
                            if (ui->lePassword->text() == ui->lePasswordVal->text() && ui->lePassword->text() != "" &&
                                    ui->lePasswordVal->text() != "")
                            {
                                QByteArray pwd;
                                QString hashedPwd;
                                GDatabase db;
                                QFile file("ep.bin");
                        
                                pwd.append(ui->lePassword->text());
                                hashedPwd = QString(QCryptographicHash::hash(pwd, QCryptographicHash::Sha3_512).toHex());
                                db.insertPassword(hashedPwd);
                                file.open(QIODevice::WriteOnly);
                                QDataStream stream(&file);
                                stream << hashedPwd;
                                file.close();
                                this->accept();
                            }
                            else
                            {
                                QMessageBox msgBox;
                                msgBox.setText("Die Passwörter stimmen nicht überein. Bitte die Eingabe überprüfen.");
                                msgBox.exec();
                            }
                        }
                        
                        1 Reply Last reply
                        0
                        • Chris KawaC Offline
                          Chris KawaC Offline
                          Chris Kawa
                          Lifetime Qt Champion
                          wrote on last edited by Chris Kawa
                          #12

                          Well you didn't do what I said. I said you should override the accept slot, not create your own.

                          If you're using the QDialogButtonBox the designer wizard adds by default then it doesn't matter what you do in your own slot. The generated code connects button box signals to the accept/reject slot, so if you call accept in your own slot it will just be called twice. If not then it will be called anyway.

                          Move that code to the overriden accept slot, change this->accept() to QDialog::accept() and it will work like you want it to.

                          Btw. Don't do that: ui->lePassword->text() != "". QString has a dedicated and faster method for checking if a string is empty - the isEmpty() method.

                          1 Reply Last reply
                          0
                          • ? Offline
                            ? Offline
                            A Former User
                            wrote on last edited by
                            #13

                            ok thx. but how do i close the dialog now without calling accept? that would be an loop. the rest works. thx for the tipp

                            1 Reply Last reply
                            0
                            • Chris KawaC Offline
                              Chris KawaC Offline
                              Chris Kawa
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              how do i close the dialog now without calling accept?

                              You do call accept. The base implementation. this->accept() would call your own implementation and it would indeed be recursive, but I said you should call QDialog::accept(), which is the base implementation, not your override. This will close the dialog.

                              1 Reply Last reply
                              0
                              • ? Offline
                                ? Offline
                                A Former User
                                wrote on last edited by
                                #15

                                Ahhhhh i got it. Thank you very much. Im happy that there are people like you. It works now and i have something i can work with. have a nice weekend.

                                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