使用qt编写代码,创建一个界面上有一个QTextEdit和一个按钮“登录”,点击登录按钮跳转到登录界面,当在登录界面用户输入用户名和密码后,点击登录,登录窗口关闭,如果用户名密码正确,程序提示,登陆成功,并在主窗
时间: 2024-05-04 14:16:34 浏览: 174
口的QTextEdit控件中显示欢迎信息。如果用户名或密码错误,则提示登陆失败,请重新输入。
好的,您需要使用Qt编写代码来实现这个功能。下面是代码示例:
```
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
// 创建主窗口
QMainWindow *mainWindow = new QMainWindow();
QTextEdit *textEdit = new QTextEdit(mainWindow);
QPushButton *loginButton = new QPushButton("登录", mainWindow);
// 设置按钮位置和大小
loginButton->setGeometry(10, 10, 80, 30);
// 创建登录窗口
QDialog *loginDialog = new QDialog(mainWindow);
QLabel *usernameLabel = new QLabel("用户名:", loginDialog);
QLabel *passwordLabel = new QLabel("密码:", loginDialog);
QLineEdit *usernameEdit = new QLineEdit(loginDialog);
QLineEdit *passwordEdit = new QLineEdit(loginDialog);
QPushButton *okButton = new QPushButton("确定", loginDialog);
// 设置控件位置和大小
usernameLabel->setGeometry(10, 10, 60, 30);
passwordLabel->setGeometry(10, 50, 60, 30);
usernameEdit->setGeometry(80, 10, 150, 30);
passwordEdit->setGeometry(80, 50, 150, 30);
okButton->setGeometry(80, 90, 80, 30);
passwordEdit->setEchoMode(QLineEdit::Password); // 设置密码输入框为密文模式
// 连接登录按钮的槽函数
QObject::connect(loginButton, &QPushButton::clicked, [&](){
loginDialog->show();
});
// 连接确定按钮的槽函数
QObject::connect(okButton, &QPushButton::clicked, [&](){
QString username = usernameEdit->text();
QString password = passwordEdit->text();
if(username == "admin" && password == "123456") {
QMessageBox::information(loginDialog, "提示", "登录成功");
mainWindow->show();
QString welcomeMsg = QString("欢迎您,%1!").arg(username);
textEdit->setText(welcomeMsg);
loginDialog->close();
}
else {
QMessageBox::warning(loginDialog, "错误", "用户名或密码错误,请重新输入!");
}
});
mainWindow->setCentralWidget(textEdit);
mainWindow->show();
return app.exec();
}
```
注:本示例中的用户名和密码都是“admin”和“123456”,您可以根据需要自行修改。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)