qt 实用登陆注册功能代码
时间: 2023-11-04 09:16:02 浏览: 47
以下是一个基于Qt框架的简单登陆注册功能代码,包括用户信息的存储和读取:
```
#include <QtWidgets>
class LoginForm : public QWidget
{
Q_OBJECT
public:
LoginForm(QWidget *parent = 0)
: QWidget(parent)
{
QLabel *usernameLabel = new QLabel(tr("Username:"));
QLabel *passwordLabel = new QLabel(tr("Password:"));
usernameLineEdit = new QLineEdit;
passwordLineEdit = new QLineEdit;
passwordLineEdit->setEchoMode(QLineEdit::Password);
loginButton = new QPushButton(tr("Login"));
connect(loginButton, SIGNAL(clicked()), this, SLOT(login()));
registerButton = new QPushButton(tr("Register"));
connect(registerButton, SIGNAL(clicked()), this, SLOT(registerUser()));
QGridLayout *layout = new QGridLayout;
layout->addWidget(usernameLabel, 0, 0);
layout->addWidget(usernameLineEdit, 0, 1);
layout->addWidget(passwordLabel, 1, 0);
layout->addWidget(passwordLineEdit, 1, 1);
layout->addWidget(loginButton, 2, 0);
layout->addWidget(registerButton, 2, 1);
setLayout(layout);
// load user info from file
QFile file("users.txt");
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine();
QStringList fields = line.split(",");
if (fields.size() == 2) {
users.insert(fields[0], fields[1]);
}
}
}
}
private slots:
void login()
{
QString username = usernameLineEdit->text();
QString password = passwordLineEdit->text();
if (users.contains(username) && users.value(username) == password) {
QMessageBox::information(this, tr("Login Successful"), tr("Welcome, %1!").arg(username));
} else {
QMessageBox::warning(this, tr("Login Failed"), tr("Invalid username or password."));
}
}
void registerUser()
{
QString username = usernameLineEdit->text();
QString password = passwordLineEdit->text();
if (username.isEmpty() || password.isEmpty()) {
QMessageBox::warning(this, tr("Registration Failed"), tr("Please enter both username and password."));
return;
}
if (users.contains(username)) {
QMessageBox::warning(this, tr("Registration Failed"), tr("Username already exists."));
return;
}
users.insert(username, password);
// save user info to file
QFile file("users.txt");
if (file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) {
QTextStream out(&file);
out << username << "," << password << endl;
}
QMessageBox::information(this, tr("Registration Successful"), tr("User %1 has been registered.").arg(username));
}
private:
QLineEdit *usernameLineEdit;
QLineEdit *passwordLineEdit;
QPushButton *loginButton;
QPushButton *registerButton;
QMap<QString, QString> users;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
LoginForm form;
form.show();
return app.exec();
}
#include "main.moc"
```
这个实例中,我们使用了QMap来存储用户名和密码,将用户信息保存在文本文件中,并在程序启动时读取。登陆和注册按钮分别调用login()和registerUser()槽函数。在registerUser()函数中,我们首先检查用户名和密码是否为空,然后再检查用户名是否已经存在,最后保存新用户信息到文件中。在login()函数中,我们检查用户名和密码是否与已有的用户信息匹配,如果匹配则弹出欢迎信息,否则弹出错误提示。
这是一个非常简单的例子,实际的登陆注册功能需要考虑更多的问题,比如加密密码、验证输入格式等等。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)