qt实现登录要求用户注册完成支持多种登录方式,登录一次以后支持记住用户名和密码的代码
时间: 2023-08-09 09:12:26 浏览: 55
好的,下面是一个简单的 Qt 实现登录功能的示例代码,包括用户注册、多种登录方式、记住用户名和密码等功能。代码中使用了 Qt 的信号和槽来实现界面和后台的交互,使用了 SQLite 数据库来保存用户信息。
首先,需要在 Qt 项目中添加 SQLite 数据库支持。在 .pro 文件中添加以下代码:
```pro
QT += sql
```
然后,在注册界面中,添加用户名、密码、确认密码等输入框,以及注册按钮。在登录界面中添加用户名、密码、登录按钮、记住密码选项等控件。以下是注册界面的代码:
```cpp
#include "registerdialog.h"
#include "ui_registerdialog.h"
#include <QMessageBox>
#include <QSqlQuery>
RegisterDialog::RegisterDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::RegisterDialog)
{
ui->setupUi(this);
// 设置密码输入框为密码模式
ui->passwordLineEdit->setEchoMode(QLineEdit::Password);
ui->confirmLineEdit->setEchoMode(QLineEdit::Password);
}
RegisterDialog::~RegisterDialog()
{
delete ui;
}
void RegisterDialog::on_registerButton_clicked()
{
// 获取用户输入的信息
QString username = ui->usernameLineEdit->text();
QString password = ui->passwordLineEdit->text();
QString confirm = ui->confirmLineEdit->text();
// 判断用户名和密码是否为空
if (username.isEmpty() || password.isEmpty() || confirm.isEmpty()) {
QMessageBox::warning(this, "警告", "用户名和密码不能为空!");
return;
}
// 判断两次输入的密码是否一致
if (password != confirm) {
QMessageBox::warning(this, "警告", "两次输入的密码不一致!");
return;
}
// 查询数据库中是否已经存在该用户名
QSqlQuery query;
query.prepare("SELECT * FROM users WHERE username = :username");
query.bindValue(":username", username);
query.exec();
if (query.next()) {
QMessageBox::warning(this, "警告", "该用户名已经被注册!");
return;
}
// 将用户信息保存到数据库中
query.prepare("INSERT INTO users (username, password) VALUES (:username, :password)");
query.bindValue(":username", username);
query.bindValue(":password", password);
if (!query.exec()) {
QMessageBox::warning(this, "警告", "注册失败!");
return;
}
// 注册成功,关闭注册界面
QMessageBox::information(this, "提示", "注册成功!");
accept();
}
```
以下是登录界面的代码:
```cpp
#include "logindialog.h"
#include "ui_logindialog.h"
#include <QMessageBox>
#include <QSqlQuery>
#include <QSettings>
LoginDialog::LoginDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::LoginDialog)
{
ui->setupUi(this);
// 设置密码输入框为密码模式
ui->passwordLineEdit->setEchoMode(QLineEdit::Password);
// 加载上一次登录时保存的用户名和密码
QSettings settings("MyApp", "Login");
QString username = settings.value("username").toString();
QString password = settings.value("password").toString();
if (!username.isEmpty()) {
ui->usernameLineEdit->setText(username);
ui->passwordLineEdit->setText(password);
ui->rememberCheckBox->setChecked(true);
}
}
LoginDialog::~LoginDialog()
{
delete ui;
}
void LoginDialog::on_loginButton_clicked()
{
// 获取用户输入的信息
QString username = ui->usernameLineEdit->text();
QString password = ui->passwordLineEdit->text();
// 判断用户名和密码是否为空
if (username.isEmpty() || password.isEmpty()) {
QMessageBox::warning(this, "警告", "用户名和密码不能为空!");
return;
}
// 查询数据库中是否存在该用户
QSqlQuery query;
query.prepare("SELECT * FROM users WHERE username = :username AND password = :password");
query.bindValue(":username", username);
query.bindValue(":password", password);
query.exec();
if (!query.next()) {
QMessageBox::warning(this, "警告", "用户名或密码错误!");
return;
}
// 登录成功,保存用户名和密码到本地
if (ui->rememberCheckBox->isChecked()) {
QSettings settings("MyApp", "Login");
settings.setValue("username", username);
settings.setValue("password", password);
}
// 关闭登录窗口
accept();
}
```
在 main 函数中,需要先创建数据库并创建用户表:
```cpp
#include "logindialog.h"
#include <QApplication>
#include <QSqlDatabase>
#include <QSqlQuery>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// 创建数据库并创建用户表
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("users.db");
if (!db.open()) {
QMessageBox::warning(nullptr, "警告", "无法创建数据库!");
return 0;
}
QSqlQuery query;
query.exec("CREATE TABLE IF NOT EXISTS users ("
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"username TEXT NOT NULL,"
"password TEXT NOT NULL)");
// 显示登录窗口
LoginDialog loginDialog;
if (loginDialog.exec() == QDialog::Accepted) {
// 登录成功,显示主窗口
// ...
return a.exec();
}
// 退出程序
return 0;
}
```
以上就是一个简单的 Qt 实现登录功能的示例代码,包括用户注册、多种登录方式、记住用户名和密码等功能。具体的实现细节还需要根据需求进行调整。
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.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)