C++ 从“内部”调用/移动函数;“如果循环”;另一个函数-Qt,C++;

C++ 从“内部”调用/移动函数;“如果循环”;另一个函数-Qt,C++;,c++,qt,login,C++,Qt,Login,我试图在qt中实现一个登录页面,但遇到了一个奇怪的问题。我想检查两种类型的密码。一个是常规密码,第二个是主密码。当用户输入错误的密码5次时,他必须输入主密码,如果他也输入了3次错误的密码,则显示错误 我已经写了代码,但面临一个无法解决的问题。这是我的登录码: void FormLogin::OnLogin() { QString password = passLineEdit->text(); // Checking if username or password is

我试图在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 loop”条件,它仍会调用整个函数:

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(尝试<5)
条件。如果达到5次尝试,则应防止进入第一个功能。

断开连接(登录按钮、信号(单击())、此插槽(OnLogin())
添加到执行
返回操作的第一个功能。谢谢:)