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++ chrono简单使用场景示例详解

《C/C++chrono简单使用场景示例详解》:本文主要介绍C/C++chrono简单使用场景示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友... 目录chrono使用场景举例1 输出格式化字符串chrono使用场景China编程举例1 输出格式化字符串示

Python验证码识别方式(使用pytesseract库)

《Python验证码识别方式(使用pytesseract库)》:本文主要介绍Python验证码识别方式(使用pytesseract库),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全... 目录1、安装Tesseract-OCR2、在python中使用3、本地图片识别4、结合playwrigh

Python使用Code2flow将代码转化为流程图的操作教程

《Python使用Code2flow将代码转化为流程图的操作教程》Code2flow是一款开源工具,能够将代码自动转换为流程图,该工具对于代码审查、调试和理解大型代码库非常有用,在这篇博客中,我们将深... 目录引言1nVflRA、为什么选择 Code2flow?2、安装 Code2flow3、基本功能演示

使用vscode搭建pywebview集成vue项目实践

《使用vscode搭建pywebview集成vue项目实践》:本文主要介绍使用vscode搭建pywebview集成vue项目实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录环境准备项目源码下载项目说明调试与生成可执行文件核心代码说明总结本节我们使用pythonpywebv

C++类和对象之默认成员函数的使用解读

《C++类和对象之默认成员函数的使用解读》:本文主要介绍C++类和对象之默认成员函数的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、默认成员函数有哪些二、各默认成员函数详解默认构造函数析构函数拷贝构造函数拷贝赋值运算符三、默认成员函数的注意事项总结一

使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)

《使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)》字体设计和矢量图形处理是编程中一个有趣且实用的领域,通过Python的matplotlib库,我们可以轻松将字体轮廓... 目录背景知识字体轮廓的表示实现步骤1. 安装依赖库2. 准备数据3. 解析路径指令4. 绘制图形关键

VS配置好Qt环境之后但无法打开ui界面的问题解决

《VS配置好Qt环境之后但无法打开ui界面的问题解决》本文主要介绍了VS配置好Qt环境之后但无法打开ui界面的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目UKeLvb录找到Qt安装目录中designer.UKeLvBexe的路径找到vs中的解决方案资源

详解如何使用Python从零开始构建文本统计模型

《详解如何使用Python从零开始构建文本统计模型》在自然语言处理领域,词汇表构建是文本预处理的关键环节,本文通过Python代码实践,演示如何从原始文本中提取多尺度特征,并通过动态调整机制构建更精确... 目录一、项目背景与核心思想二、核心代码解析1. 数据加载与预处理2. 多尺度字符统计3. 统计结果可

Linux基础命令@grep、wc、管道符的使用详解

《Linux基础命令@grep、wc、管道符的使用详解》:本文主要介绍Linux基础命令@grep、wc、管道符的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录grep概念语法作用演示一演示二演示三,带选项 -nwc概念语法作用wc,不带选项-c,统计字节数-

SpringCloud中的@FeignClient注解使用详解

《SpringCloud中的@FeignClient注解使用详解》在SpringCloud中使用Feign进行服务间的调用时,通常会使用@FeignClient注解来标记Feign客户端接口,这篇文章... 在Spring Cloud中使用Feign进行服务间的调用时,通常会使用@FeignClient注解