Switch to another Form



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


  • Moderators

    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();
    }
    


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



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


  • Moderators

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



  • Thanks. I take a look at QDialog



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


  • Moderators

    @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);
    }
    


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


  • Moderators

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



  • 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();
        }
    }
    

  • Moderators

    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.



  • 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


  • Moderators

    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.



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.