用qt写一个简单的登录界面
时间: 2023-10-20 21:05:34 浏览: 46
好的,下面是一个使用Qt编写的简单登录界面的示例代码:
```cpp
#include <QtWidgets/QApplication>
#include <QtWidgets/QWidget>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QMessageBox>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
// 创建主窗口
QWidget window;
window.setWindowTitle("登录");
// 创建用户名和密码的标签和文本框
QLabel *usernameLabel = new QLabel("用户名:", &window);
QLineEdit *usernameLineEdit = new QLineEdit(&window);
QLabel *passwordLabel = new QLabel("密码:", &window);
QLineEdit *passwordLineEdit = new QLineEdit(&window);
passwordLineEdit->setEchoMode(QLineEdit::Password);
// 创建登录按钮
QPushButton *loginButton = new QPushButton("登录", &window);
// 设置布局
QVBoxLayout *layout = new QVBoxLayout(&window);
layout->addWidget(usernameLabel);
layout->addWidget(usernameLineEdit);
layout->addWidget(passwordLabel);
layout->addWidget(passwordLineEdit);
layout->addWidget(loginButton);
// 连接登录按钮的点击事件
QObject::connect(loginButton, &QPushButton::clicked, [&]() {
QString username = usernameLineEdit->text();
QString password = passwordLineEdit->text();
if (username == "admin" && password == "123456") {
QMessageBox::information(&window, "登录成功", "欢迎,管理员!");
} else {
QMessageBox::warning(&window, "登录失败", "用户名或密码错误!");
}
});
// 显示窗口
window.setLayout(layout);
window.show();
return app.exec();
}
```
这个示例中创建了一个简单的登录界面,包括用户名、密码的标签和文本框,以及一个登录按钮。当点击登录按钮时,会检查用户名和密码是否正确,并在对话框中显示相应的结果。你可以根据需要进行修改和扩展。编译运行这段代码后,就可以看到登录界面了。