从另一个函数的"if loop"中调用/移动一个函数 - Qt,C++

Call/move a function from inside an "if loop" of another function - Qt, C++

本文关键字:函数 一个 Qt C++ if 另一个 loop 调用 移动      更新时间:2023-10-16

我试图在qt中实现一个登录页面,但遇到了一个奇怪的问题。我想检查两种类型的密码。一个是常规密码,第二个将是主密码。当使用者输入错误的密码5次时,他必须输入主密码,如果他也输入了3次错误的密码,则显示器上将出现错误。

我已经编写了代码,但遇到了一个无法解决的问题。这是我的登录代码:

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."));}}
}

只有当第一个函数中的尝试次数等于5时,我才想调用第二个函数。但在5个循环之后,我的代码调用了第二个函数,但它同时运行了第一个函数和第二个功能。有人能告诉我哪里做错了吗?我试图将函数组合在一起,并试图将第二个函数用作第一个函数中的嵌套循环,但即使我将其设置在"if循环"条件中,它仍然调用整个函数:

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."));}}}
}       
}

使用以下代码调用第一个函数:

connect(loginButton, SIGNAL(clicked()), this, SLOT(OnLogin()));

任何建议都将不胜感激。

我假设您已经将loginButton::clicked()连接到FormLogin::OnLogin()。在该方法中,在五次尝试时,另一个连接添加到FormLogin::OnMasterLogin(),但保留原始连接。如果当前处于"主登录"模式,请使用disconnect()或向FormLogin::OnLogin()添加逻辑以退出。

您可以在调用第一个函数之前添加一个if (attempt < 5)条件。如果达到5次尝试,则应防止进入第一个动作。

相关文章: