我想在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()));
任何建議值得高度讚賞。
將'disconnect(loginButton,SIGNAL(clicked()),this,SLOT(OnLogin()))'添加到返回工作的第一個函數中。謝謝 :) – John