gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 04:57:42 25 4
gpt4 key购买 nike

我试图在 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() 添加逻辑以退出。

关于c++ - 从另一个函数的 "if loop"内部调用/移动一个函数 - Qt,C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46803890/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com