从另一个函数的"if loop"中调用/移动一个函数 - Qt,C++
Call/move a function from inside an "if loop" of another function - Qt, C++
我试图在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次尝试,则应防止进入第一个动作。
相关文章:
- 函数向量_指针有不同的原型,我可以构建一个吗
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 如何仅为一个函数添加延迟
- 构造函数正在调用一个使用当前类类型的函数
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 我不明白为什么我声明一个空的内部结构并将其传递给构造函数
- 如何创建函数管道,以便函数一个接一个地运行?
- 如何巧妙地编写两个函数——一个用于检查是否存在解决方案,另一个用于获取所有解决方案
- 在c++中的复制构造函数/一个声明语句中的初始化的延续中使用chain方法
- C :基类调用自己的虚拟函数 - 一个反图案
- 如何在这个交换函数(一个单独的链表)中找到错误
- 两个相同的函数(一个使用模板模式,另一个不使用)
- 你怎么能一次给一个函数一个参数呢
- 为什么要做两个函数?(一个是非const,另一个是const)
- 当代码在其他地方使用时,如何保证函数一个接一个地被调用