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