2017-10-18 242 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次嘗試,這應該防止進入第一個功能。

相關問題