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.
-
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
inheritsQWidget
, but I think theQDialog::DialogCode
enum and the related methods are introduced inQDialog
. Have a look at the QDialog docs. -
@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 implementsuccessfullyLogged
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 callaccept()
if the login was successful orreject()
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?
-
@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.
-
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(); } }
-
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, changethis->accept()
toQDialog::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 - theisEmpty()
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
-
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 callQDialog::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.