2017-10-18 199 views
0

我想在qt中实现登录页面,并陷入奇怪的问题。我想检查两种类型的密码。一个是普通密码,第二个是主密码。当用户输入5次密码错误时,必须输入主密码,如果输入3次密码错误,则显示错误。从另一个函数的“if循环”中调用/移动函数-Qt,C++

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

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())); 

任何建议值得高度赞赏。

回答

1

我假设你已经将loginButton :: clicked()连接到了FormLogin :: OnLogin()。在该方法中,在五次尝试中,您将添加到另一个连接到FormLogin :: OnMasterLogin(),但您仍保留原始连接。使用disconnect()或向FormLogin :: OnLogin()添加逻辑以释放当前处于“主登录”模式下的状态。

+0

将'disconnect(loginButton,SIGNAL(clicked()),this,SLOT(OnLogin()))'添加到返回工作的第一个函数中。谢谢 :) – John

0

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

相关问题