J'essaye d'implémenter une page de connexion en qt et je suis coincé dans un problème bizarre. Je veux vérifier 2 types de mots de passe. L'un est le mot de passe normal et le second sera le mot de passe principal. Lorsque le mot de passe entré 5 fois erroné, il doit entrer le mot de passe maître et s'il entre également 3 mot de passe erroné, il y aura une erreur sur l'affichage.Appeler/déplacer une fonction depuis l'intérieur d'une "if loop" d'une autre fonction - Qt, C++
J'ai écrit le code, mais je suis confronté à un problème que je suis incapable de résoudre. Voici mon code de connexion:
void FormLogin::OnLogin()
{
QString password = passLineEdit->text();
// Checking if username or password is empty
if (password.isEmpty())
{QMessageBox::information(this, tr("Warning!"), "Password field is empty!");
} else if (password == "pass")
{this->destroy();
} else {
QMessageBox::information(this, tr("Warning!"), QString("Wrong password!!! Only %1 attempt(s) left!").arg(4-attempt));
attempt++;
if (attempt == 5){
QMessageBox::information(this, tr("Warning!"), QString("The device is locked due to too many failed attempts. Please enter the master password to unlock the device now."));
connect(loginButton, SIGNAL(clicked()), this, SLOT(OnMasterLogin()));
return;}
}
}
void FormLogin::OnMasterLogin()
{
QString mpassword = passLineEdit->text();
// Checking if username or password is empty
if (mpassword.isEmpty())
{QMessageBox::information(this, tr("Warning!"), "MPassword field is empty!");
} else if (mpassword == "masterpass")
{this->destroy();
} else {
QMessageBox::information(this, tr("Warning!"), QString("Wrong mpassword!!! Only %1 attempt(s) left!").arg(2-master_attempt));
master_attempt++;
if (master_attempt == 3){
QMessageBox::information(this, tr("Warning!"), QString("The device is permanently locked due to too many failed attempts. Please contact the device manufacturer."));}}
}
Je veux appeler la deuxième fonction que lorsque la tentative de la première fonction est égale à 5. Mais après 5 boucles, mon code appelle la deuxième fonction, mais il fonctionne alors la première fonction et deuxième fonction simultanément. Quelqu'un peut-il me dire où je me trompe? J'ai essayé de combiner la fonction ensemble et essayé d'utiliser la deuxième fonction de boucles imbriquées à l'intérieur du premier, mais il appelle toujours la fonction entière, même si je l'ai mis dans une « boucle si » condition:
void FormLogin::OnLogin()
{
QString password = passLineEdit->text();
// Checking if username or password is empty
if (password.isEmpty())
{QMessageBox::information(this, tr("Warning!"), "Password field is empty!");
} else if (password == "pass")
{this->destroy();
} else {
QMessageBox::information(this, tr("Warning!"), QString("Wrong password!!! Only %1 attempt(s) left!").arg(4-attempt));
attempt++;
if (attempt == 5){
QMessageBox::information(this, tr("Warning!"), QString("The device is locked due to too many failed attempts. Please enter the master password to unlock the device now."));
QString mpassword = passLineEdit->text();
// Checking if username or password is empty
if (mpassword.isEmpty())
{QMessageBox::information(this, tr("Warning!"), "MPassword field is empty!");
} else if (mpassword == "masterpass")
{this->destroy();
} else {
QMessageBox::information(this, tr("Warning!"), QString("Wrong mpassword!!! Only %1 attempt(s) left!").arg(2-master_attempt));
master_attempt++;
if (master_attempt == 3){
QMessageBox::information(this, tr("Warning!"), QString("The device is permanently locked due to too many failed attempts. Please contact the device manufacturer."));}}}
}
}
Le La première fonction est appelée en utilisant le code suivant:
connect(loginButton, SIGNAL(clicked()), this, SLOT(OnLogin()));
Toute suggestion est fortement appréciée.
Ajout de 'disconnect (loginButton, SIGNAL (clicked)), this, SLOT (OnLogin()))' à la première fonction où 'return' a été travaillé. Merci :) – John