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

news2024/10/4 22:23:55

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 excle
    QString 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_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    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_OBJECT

public:
    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 sql

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += 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.0

SOURCES += \
    main.cpp \
    mainwindow.cpp \
    signup.cpp

HEADERS += \
    mainwindow.h \
    signup.h

FORMS += \
    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 += target

RESOURCES += \
    images.qrc

图片资源

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1124813.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

AUTOSAR通信篇 - CAN网络通信(七:Nm)

文章目录 基础功能NM协调器功能NM协调器功能的适用性保持协调总线活动总线关闭的协调嵌套子总线的协调关闭定时器的计算同步用例1 – 同步指令同步用例2-同步启动同步用例3 -同步网络睡眠示例 唤醒和中止协调关闭外部的网络唤醒协调唤醒协调关闭的中止 部分网络功能PNC位向量过…

高速电路设计----第三章(2)LVDS信号详解

一、TTL和CMOS不适用于高速电路设计的原因&#xff08;都是数字电路信号&#xff09; 原因&#xff1a; ①电平幅度较大&#xff0c;电平最低都达到了2.5V或者3.3V。因此信号沿变化所需要的时间很长。不适合大于200MHZ的信号。 ②容易被干扰&#xff0c;输出信号为单端&#xf…

Ubuntu OpenLDAP配置笔记

Ubuntu OpenLDAP配置笔记 问题&#xff08;需求&#xff09;LDAP服务端安装slapd和ldap-utils配置域名编辑hosts修改主机名验证增加一个域账号修改用户的密码 Linux桌面加域安装软件验证允许远程账号首次登录时自动创建HOME目录桌面登录 其它问题Ubuntu更新和安装太慢LDAP服务端…

安装了WinRAR,但是右键发现没有压缩选项,怎么办

我们安装了WinRAR之后想要压缩文件&#xff0c;但是右键点击文件之后发现并没有WinRAR压缩选项&#xff0c;这应该如何设置才能出现右键带有压缩选项呢&#xff1f;方法如下&#xff1a; 首先打开WinRAR&#xff0c;在上面功能中点击选项 – 设置 然后我们在设置界面中切换到集…

基于Restful的WebService

目录 Restful简介 1. 资源(Resources) 2. 表述性状态转移(Representational State Transfer) 3. URL(统一资源定位符) 4. 数据格式(Data Format) 5. 状态码(Status Codes) 6. 超媒体(Hypermedia) 7. 无状态性(Statelessness) 8. 资源关系(Resource Relationships) 9.…

WinCC趋势跨度设置(时间范围)

控件&#xff1a;输入输出域、组合框、按钮、实时趋势控件 输入输出域 对象名称&#xff1a;IOI 域类型&#xff1a;输入 组合框 对象名称&#xff1a;cb 索引与文本一一对应 按钮VB Sub OnClick(Byval Item) …

RustCC分享会|非凸科技与开发者共同探讨Rust安全进化

10月15日&#xff0c;非凸科技受邀参加RustCC联合多家开发者社区组织的Global Tour of Rust技术分享活动&#xff0c;旨在为Rust开发者提供交流互动的平台&#xff0c;分享Rust语言的知识、经验和最佳实践。 活动上&#xff0c;非凸科技成都分公司研发总监赵海峰以“Rust安全进…

【框架源码篇 05】Spring源码篇-ApplicationContext

Spring源码篇-ApplicationContext 前面通过手写IoC&#xff0c;DI、AOP和Bean的配置。到最后ApplicationContext的门面处理&#xff0c;对于Spring相关的核心概念应该会比较清楚了。接下来我们就看看在Spring源码中&#xff0c;对于的核心组件是如何实现的。 一、ApplicationC…

光环云入选“北京市算力互联互通试点参与企业”!

为进一步贯彻落实工业和信息化部等六部委联合印发的《算力基础设施高质量发展行动计划》&#xff0c;扩大北京市算力互联互通试点参与范围&#xff0c;助力建设全球数字经济标杆城市&#xff0c;北京市通信管理局组织相关专家对申报第二批参与试点企业开展评估&#xff0c;光环…

docker 部署服务案例

mysql Centos7为例 NAME"CentOS Linux" VERSION"7 (Core)" ID"centos" ID_LIKE"rhel fedora" VERSION_ID"7" PRETTY_NAME"CentOS Linux 7 (Core)" ANSI_COLOR"0;31" CPE_NAME"cpe:/o:centos:cento…

mysql之通过表名来搜索库名

1、经常遇到查日志时候知道表名&#xff0c;但是不知道在哪个库下面&#xff0c;可以通过此sql语句查询。 SELECT * FROM information_schema.TABLES WHERE table_name tb_xxxxxx;

python之代理ip的配置与调试方法详解

代理IP在Python中是一种强大的工具&#xff0c;它可以用于隐藏真实IP地址、绕过访问限制、提高数据爬取和网络请求的效率等。下面将详细介绍Python中代理IP的配置与调试方法&#xff0c;帮助您更好地理解和应用代理IP。 1. 选择合适的代理IP 在使用代理IP之前&#xff0c;需要…

vtk 多边形绘制 vtkPolygon 三角形 矩形 多边形

vtk 可以通过 vtkPolygon 绘制 三角形 矩形 多边形 目录 vtk 可以通过 vtkPolygon 绘制 三角形 矩形 多边形 效果&#xff1a; 源码&#xff1a; 效果&#xff1a; 三角形 矩形&#xff1a; 多边形&#xff1a; 源码&#xff1a; #include "vtkAutoInit.h" VTK_M…

通过电脑操作安卓手机数据恢复最好的几个工具

在本次评测中&#xff0c;我将介绍适用于 PC (Windows 10/11) 的最佳 Android 数据恢复软件&#xff0c;它可以帮助您从通过 MTP 连接的手机或平板电脑上恢复文件和数据。 2023 年适用于 PC 和 Mac 的最佳安卓数据恢复软件 1、U1tData安卓数据恢复&#xff08;奇客软件&#xf…

599L是什么芯片,sot23-6封装591NW

60转5v-599L芯片具有以下特点&#xff1a; - 600mA的连续输出电流能力 - 宽输入工作范围&#xff0c;从4.5V至60V - 集成了80V、550mQ高侧和80V、350mQ低侧功率MOSFET开关 - 高达95%的效率 - 内部软启动功能&#xff0c;限制开机时的浪涌电流 - 内部补偿功能&#xff0c;减少外…

程序员空闲时间能不能找其他副业?还是该继续卷技术?

我认为这是个伪命题&#xff01;程序员都在996/007&#xff0c;哪来的空闲时间&#xff0c;这是让他们不睡觉吗&#xff1f; 那些有空闲时间的程序员应该是还没找到工作的程序员吧&#xff01;主业都没有&#xff0c;谈什么副业。 假如有少部分人&#xff0c;是属于985的程序…

超实用的跟圈和一键转发好友朋友圈功能

一键转发朋友圈/跟圈 想转发别人的朋友圈内容&#xff0c;通常需要手动复制粘贴&#xff0c;一个个复制保存实在是太麻烦耗费时间。 有时候咱也不可能随时都看朋友圈嘛&#xff0c;那又想及时转发朋友的圈的&#xff0c;有什么办法可以轻松实现呢&#xff1f; 操作步骤 单击…

黑客为什么不 入侵银行一夜暴富

前言 厉害的黑客只需要入侵银行系统&#xff0c;改动一下自己账户余额数字&#xff0c;身家不就可以轻松过亿了吗&#xff1f; 然而事实却是没有哪个黑客做成这件事。大家要知道&#xff0c;银行的网站和网上银行系统是分开部署的。黑客成功入侵了银行的网站&#xff0c;他能…

推荐一款可以识别m3u8格式ts流批量下载并且合成mp4视频的chrome插件——猫抓

https://chrome.google.com/webstore/detail/%E7%8C%AB%E6%8A%93/jfedfbgedapdagkghmgibemcoggfppbb?utm_sourceext_app_menuhttps://chrome.google.com/webstore/detail/%E7%8C%AB%E6%8A%93/jfedfbgedapdagkghmgibemcoggfppbb?utm_sourceext_app_menu 网页媒体嗅探工具 一…

(十一)Python模块和包

前面章节中&#xff0c;我们已经使用了很多模块&#xff08;如 string、sys、os 等&#xff09;&#xff0c;通过向程序中导入这些模块&#xff0c;我们可以使用很多“现成”的函数实现想要的功能。 那么&#xff0c;模块到底是什么&#xff0c;模块内部到底是什么样子的&…