请使用Qt满足一个登录窗口 两个文本输入(账号 密码)和一个登录按键 要求有密码检测机制 同时正确的账号和密码保存在txt文件
时间: 2024-03-24 09:37:49 浏览: 24
好的,我可以为您提供一个简单的代码示例。首先,您需要在Qt中创建一个新的项目,并添加一个登录窗口,该窗口包含两个文本输入框和一个登录按钮。接下来,您需要将正确的用户名和密码存储在txt文件中。
下面是一个简单的代码示例,用于创建一个登录窗口,并将用户名和密码存储在txt文件中:
```cpp
#include <QtWidgets>
// 定义文件名
#define FILE_NAME "login.txt"
class LoginWindow : public QWidget {
public:
LoginWindow(QWidget *parent = nullptr) : QWidget(parent) {
// 设置窗口标题
setWindowTitle(tr("登录窗口"));
// 创建账号输入框
m_accountEdit = new QLineEdit(this);
m_accountEdit->setPlaceholderText(tr("请输入账号"));
// 创建密码输入框
m_passwordEdit = new QLineEdit(this);
m_passwordEdit->setPlaceholderText(tr("请输入密码"));
m_passwordEdit->setEchoMode(QLineEdit::Password);
// 创建登录按钮
m_loginButton = new QPushButton(tr("登录"), this);
// 设置布局
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(m_accountEdit);
layout->addWidget(m_passwordEdit);
layout->addWidget(m_loginButton);
// 将用户名和密码写入到文件中
QFile file(FILE_NAME);
if (file.open(QIODevice::ReadWrite | QIODevice::Text)) {
QTextStream stream(&file);
stream << "admin admin" << endl;
} else {
QMessageBox::critical(this, tr("错误"), tr("无法打开文件!"));
return;
}
// 连接登录按钮的槽函数
connect(m_loginButton, &QPushButton::clicked, this, &LoginWindow::onLoginButtonClicked);
}
private:
QLineEdit *m_accountEdit;
QLineEdit *m_passwordEdit;
QPushButton *m_loginButton;
void onLoginButtonClicked() {
// 获取输入的用户名和密码
QString username = m_accountEdit->text();
QString password = m_passwordEdit->text();
// 从文件中读取正确的用户名和密码
QFile file(FILE_NAME);
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream stream(&file);
QString line;
while (!stream.atEnd()) {
line = stream.readLine();
QStringList list = line.split(" ");
if (list.size() == 2 && list.at(0) == username && list.at(1) == password) {
QMessageBox::information(this, tr("提示"), tr("登录成功!"));
return;
}
}
} else {
QMessageBox::critical(this, tr("错误"), tr("无法打开文件!"));
return;
}
// 如果用户名或密码错误,则登录失败
QMessageBox::warning(this, tr("警告"), tr("用户名或密码错误!"));
}
};
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
LoginWindow window;
window.show();
return app.exec();
}
```
在上面的代码中,我们首先将正确的用户名为`admin`,密码为`admin`的用户写入到txt文件中。
在登录按钮被点击时,我们从输入框中获取用户名和密码,并从文件中读取正确的用户名和密码。如果存在该用户,则登录成功,否则提示用户名或密码错误。
注意,上面的代码仅供参考,您需要根据自己的实际需求进行修改和完善。
相关推荐
![](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)