Qt Creater 设计的登录注册界面 使用SQLite数据库

2023-10-24 03:28

本文主要是介绍Qt Creater 设计的登录注册界面 使用SQLite数据库,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Qt Creater 设计的登录注册界面 使用SQLite数据库

案例截图

登录页面登录页面

注册页面注册页面

项目目录结构截图

目录结构

代码

main.cpp
#include "mainwindow.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);MainWindow w;//第一个是去掉原边框及标题栏,第二个是保留最小化及还原功能,主要是为了还原,最小化下面实现了w.setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint);w.show();return a.exec();
}

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "signup.h"
#include <QGraphicsDropShadowEffect>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);//设置图片QPixmap *pix = new QPixmap(":/images/blue.png");QSize sz = ui->label_image->size();ui->label_image->setPixmap(pix->scaled(sz));//设置图片阴影效果QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);shadow->setOffset(-3, 0);shadow->setColor(QColor(136,136,136));shadow->setBlurRadius(30);ui->label_image->setGraphicsEffect(shadow);
}MainWindow::~MainWindow()
{delete ui;
}void sqlite_Init()
{QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");db.setDatabaseName("user.db");if(!db.open()){qDebug()<<"open error";}//create excleQString createsql=QString("create table if not exists user(id integer primary key autoincrement,""username ntext unique not NULL,""password ntext not NULL)");QSqlQuery query;if(!query.exec(createsql)){qDebug()<<"table create error";}else{qDebug()<<"table create success";}
}void MainWindow::on_btn_signin_clicked()
{sqlite_Init();QString username = ui->lineEdit_username->text();QString password = ui->lineEdit_password->text();QString sql=QString("select * from user where username='%1' and password='%2'").arg(username,password);//创建执行语句对象QSqlQuery query(sql);//判断执行结果if(!query.next()){qDebug()<<"Login error";QMessageBox::information(this,"登录认证","登录失败,账户或者密码错误");}else{qDebug()<<"Login success";QMessageBox::information(this,"登录认证","登录成功");//登录成功后可以跳转到其他页面QWidget *w = new QWidget;w->show();this->close();}
}void MainWindow::on_btn_signup_clicked()
{this->close();Signup *s = new Signup;s->show();
}void MainWindow::on_btn_close_clicked()
{this->close();
}

signup.cpp

#include "signup.h"
#include "ui_signup.h"
#include "mainwindow.h"Signup::Signup(QWidget *parent) :QWidget(parent),ui(new Ui::Signup)
{ui->setupUi(this);//设置图片QPixmap *pix = new QPixmap(":/images/women.png");QSize sz = ui->label_image->size();ui->label_image->setPixmap(pix->scaled(sz));
}Signup::~Signup()
{delete ui;
}void Signup::on_btn_return_clicked()
{MainWindow *w = new MainWindow;w->show();this->close();
}void Signup::on_btn_sure_clicked()
{sqlite_Init();QString username = ui->lineEdit_username->text();QString password = ui->lineEdit_passwd->text();QString surepass = ui->lineEdit_surepasswd->text();//判断密码是否一致if(password == surepass){QString sql=QString("insert into user(username,password) values('%1','%2');").arg(username).arg(password);//创建执行语句对象QSqlQuery query;//判断执行结果if(!query.exec(sql)){qDebug()<<"insert into error";QMessageBox::information(this,"注册认证","插入失败!");}else{qDebug()<<"insert into success";QMessageBox::information(this,"注册认证","插入成功!");MainWindow *w = new MainWindow;w->show();this->close();}}else{QMessageBox::information(this,"注册认证","两次密码输入不一致");}
}

页面UI

mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"><class>MainWindow</class><widget class="QMainWindow" name="MainWindow"><property name="geometry"><rect><x>0</x><y>0</y><width>780</width><height>520</height></rect></property><property name="windowTitle"><string>MainWindow</string></property><property name="styleSheet"><string notr="true">background-color: rgb(255, 255, 255);</string></property><widget class="QWidget" name="centralwidget"><widget class="QLabel" name="label_image"><property name="geometry"><rect><x>385</x><y>10</y><width>380</width><height>500</height></rect></property><property name="text"><string/></property></widget><widget class="QLabel" name="label"><property name="geometry"><rect><x>10</x><y>10</y><width>200</width><height>20</height></rect></property><property name="text"><string>切换页面</string></property></widget><widget class="QLabel" name="label_2"><property name="geometry"><rect><x>60</x><y>80</y><width>200</width><height>40</height></rect></property><property name="styleSheet"><string notr="true">font: 87 20pt &quot;仿宋&quot;;</string></property><property name="text"><string>现在登录</string></property></widget><widget class="QPushButton" name="btn_signin"><property name="geometry"><rect><x>70</x><y>400</y><width>80</width><height>24</height></rect></property><property name="styleSheet"><string notr="true">background-color: qlineargradient(spread:pad, x1:0.52, y1:1, x2:0.54, y2:0, 
stop:0.0112994 rgba(64, 145, 252, 255), 
stop:1 rgba(255, 255, 255, 255));
color: rgb(255, 255, 255);border:0px groove gray;border-radius:
7px;padding:2px 4px;
font: 14pt &quot;Candara&quot;;</string></property><property name="text"><string>登陆</string></property></widget><widget class="QPushButton" name="btn_signup"><property name="geometry"><rect><x>210</x><y>400</y><width>80</width><height>24</height></rect></property><property name="styleSheet"><string notr="true">background-color: qlineargradient(spread:pad, x1:0.52, y1:1, x2:0.54, y2:0, 
stop:0.0112994 rgba(64, 145, 252, 255), 
stop:1 rgba(255, 255, 255, 255));
color: rgb(255, 255, 255);border:0px groove gray;border-radius:
7px;padding:2px 4px;
font: 14pt &quot;Candara&quot;;</string></property><property name="text"><string>注册</string></property></widget><widget class="QLineEdit" name="lineEdit_username"><property name="geometry"><rect><x>60</x><y>230</y><width>240</width><height>40</height></rect></property><property name="styleSheet"><string notr="true">background-color: rgb(247, 247, 247);
border:1px groove gray;border-radius:
7px;padding:2px 4px;
font: 10pt &quot;Candara&quot;;</string></property><property name="placeholderText"><string>输入账号</string></property></widget><widget class="QLineEdit" name="lineEdit_password"><property name="geometry"><rect><x>60</x><y>300</y><width>240</width><height>40</height></rect></property><property name="styleSheet"><string notr="true">background-color: rgb(247, 247, 247);
border:1px groove gray;border-radius:
7px;padding:2px 4px;
font: 10pt &quot;Candara&quot;;</string></property><property name="placeholderText"><string>输入密码</string></property></widget><widget class="QPushButton" name="btn_close"><property name="geometry"><rect><x>725</x><y>15</y><width>36</width><height>36</height></rect></property><property name="styleSheet"><string notr="true">background-color:transparent;
image: url(:/images/exit.png);</string></property><property name="text"><string/></property></widget></widget></widget><resources/><connections/>
</ui>

signup.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"><class>Signup</class><widget class="QWidget" name="Signup"><property name="geometry"><rect><x>0</x><y>0</y><width>790</width><height>520</height></rect></property><property name="windowTitle"><string>Form</string></property><property name="styleSheet"><string notr="true">background-color: rgb(255, 255, 255);</string></property><widget class="QLabel" name="label_image"><property name="geometry"><rect><x>15</x><y>15</y><width>380</width><height>490</height></rect></property><property name="text"><string/></property></widget><widget class="QLabel" name="label_2"><property name="geometry"><rect><x>470</x><y>20</y><width>131</width><height>31</height></rect></property><property name="styleSheet"><string notr="true">font: 87 20pt &quot;Arial Black&quot;;</string></property><property name="text"><string>你好!</string></property></widget><widget class="QLabel" name="label_3"><property name="geometry"><rect><x>470</x><y>60</y><width>261</width><height>41</height></rect></property><property name="styleSheet"><string notr="true">font: 87 20pt &quot;Arial Black&quot;;</string></property><property name="text"><string>欢迎加入我们</string></property></widget><widget class="QLabel" name="label_4"><property name="geometry"><rect><x>470</x><y>140</y><width>131</width><height>16</height></rect></property><property name="styleSheet"><string notr="true">font: 10pt &quot;Arial&quot;;</string></property><property name="text"><string>用户名:</string></property></widget><widget class="QLabel" name="label_5"><property name="geometry"><rect><x>470</x><y>200</y><width>131</width><height>16</height></rect></property><property name="styleSheet"><string notr="true">font: 10pt &quot;Arial&quot;;</string></property><property name="text"><string>密码:</string></property></widget><widget class="QLabel" name="label_6"><property name="geometry"><rect><x>470</x><y>260</y><width>131</width><height>16</height></rect></property><property name="styleSheet"><string notr="true">font: 10pt &quot;Arial&quot;;</string></property><property name="text"><string>再次输入密码:</string></property></widget><widget class="QLineEdit" name="lineEdit_username"><property name="geometry"><rect><x>470</x><y>160</y><width>200</width><height>30</height></rect></property></widget><widget class="QLineEdit" name="lineEdit_passwd"><property name="geometry"><rect><x>470</x><y>220</y><width>200</width><height>30</height></rect></property></widget><widget class="QLineEdit" name="lineEdit_surepasswd"><property name="geometry"><rect><x>470</x><y>290</y><width>200</width><height>30</height></rect></property></widget><widget class="QPushButton" name="btn_sure"><property name="geometry"><rect><x>470</x><y>350</y><width>111</width><height>24</height></rect></property><property name="styleSheet"><string notr="true">background-color: rgb(29, 123, 255);
color: rgb(255, 255, 255);
font: 25 9pt &quot;Bahnschrift Light&quot;;</string></property><property name="text"><string>确定</string></property></widget><widget class="QPushButton" name="btn_return"><property name="geometry"><rect><x>470</x><y>400</y><width>111</width><height>24</height></rect></property><property name="styleSheet"><string notr="true">background-color: rgb(29, 123, 255);
color: rgb(255, 255, 255);
font: 25 9pt &quot;Bahnschrift Light&quot;;</string></property><property name="text"><string>返回登录</string></property></widget></widget><resources/><connections/>
</ui>

头文件

mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>#include <QSqlDatabase>
#include <QSqlQuery>
#include <QMessageBox>
#include <QDebug>void sqlite_Init();QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();private slots:void on_btn_signin_clicked();void on_btn_signup_clicked();void on_btn_close_clicked();private:Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

signup.h

#ifndef SIGNUP_H
#define SIGNUP_H#include <QWidget>namespace Ui {
class Signup;
}class Signup : public QWidget
{Q_OBJECTpublic:explicit Signup(QWidget *parent = nullptr);~Signup();private slots:void on_btn_return_clicked();void on_btn_sure_clicked();private:Ui::Signup *ui;
};#endif // SIGNUP_H

项目文件 Login.pro

QT       += core gui sqlgreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++17# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0SOURCES += \main.cpp \mainwindow.cpp \signup.cppHEADERS += \mainwindow.h \signup.hFORMS += \mainwindow.ui \signup.ui# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += targetRESOURCES += \images.qrc

图片资源

登录页面图
按钮
close按钮
注册页面图
women

这篇关于Qt Creater 设计的登录注册界面 使用SQLite数据库的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/272384

相关文章

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

Python调用Orator ORM进行数据库操作

《Python调用OratorORM进行数据库操作》OratorORM是一个功能丰富且灵活的PythonORM库,旨在简化数据库操作,它支持多种数据库并提供了简洁且直观的API,下面我们就... 目录Orator ORM 主要特点安装使用示例总结Orator ORM 是一个功能丰富且灵活的 python O

Java中String字符串使用避坑指南

《Java中String字符串使用避坑指南》Java中的String字符串是我们日常编程中用得最多的类之一,看似简单的String使用,却隐藏着不少“坑”,如果不注意,可能会导致性能问题、意外的错误容... 目录8个避坑点如下:1. 字符串的不可变性:每次修改都创建新对象2. 使用 == 比较字符串,陷阱满

Python使用国内镜像加速pip安装的方法讲解

《Python使用国内镜像加速pip安装的方法讲解》在Python开发中,pip是一个非常重要的工具,用于安装和管理Python的第三方库,然而,在国内使用pip安装依赖时,往往会因为网络问题而导致速... 目录一、pip 工具简介1. 什么是 pip?2. 什么是 -i 参数?二、国内镜像源的选择三、如何

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

Linux使用nload监控网络流量的方法

《Linux使用nload监控网络流量的方法》Linux中的nload命令是一个用于实时监控网络流量的工具,它提供了传入和传出流量的可视化表示,帮助用户一目了然地了解网络活动,本文给大家介绍了Linu... 目录简介安装示例用法基础用法指定网络接口限制显示特定流量类型指定刷新率设置流量速率的显示单位监控多个

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没