while(query.next()) { if(login::number=="0"){ QMessageBox::warning(this, "登录失败", "用户名不可为初始0"); break; } else{ //根据权限进入对应界面 if(query.value(0).toString()==password&&query.value(1).toString()=="学生"){ this->close(); Student* w2=new Student(); w2->show(); break; } else if(query.value(0).toString()==password&&query.value(1).toString()=="管理员"){ this->close(); MainWindow* w2=new MainWindow(); w2->show(); break; } else{ QMessageBox::warning(this, "登录失败", "学号或者密码错误"); break; } } }在输入错误的密码事不会弹出提示警告要怎么解决
时间: 2024-02-10 22:09:30 浏览: 23
在上述代码中,如果输入错误的密码,程序会弹出一个警告框,但是在用户关闭警告框后,程序还是会继续执行下去,这可能导致一些问题。为了解决这个问题,可以在弹出警告框后使用`return`语句来退出函数,而不是使用`break`语句跳出`while`循环。这样,如果密码错误,程序就会在弹出警告框后立即退出函数,而不会继续执行下去。
修改后的代码如下:
```
while(query.next()) {
if(login::number=="0") {
QMessageBox::warning(this, "登录失败", "用户名不可为初始0");
return; // 退出函数
}
else {
//根据权限进入对应界面
if(query.value(0).toString() == password && query.value(1).toString() == "学生") {
this->close();
Student* w2 = new Student();
w2->show();
return; // 退出函数
}
else if(query.value(0).toString() == password && query.value(1).toString() == "管理员") {
this->close();
MainWindow* w2 = new MainWindow();
w2->show();
return; // 退出函数
}
else {
QMessageBox::warning(this, "登录失败", "学号或者密码错误");
return; // 退出函数
}
}
}
```
这样,无论弹出多少次警告框,程序都会在警告框关闭后立即退出函数,而不会继续执行下去。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)