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


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

    案例截图

    登录页面登录页面

    注册页面注册页面

    项目目录结构截图

    目录结构

    代码

    main.cpp
    #include "mainwindow.h"
    
    #include 
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        //第一个是去掉原边框及标题栏,第二个是保留最小化及还原功能,主要是为了还原,最小化下面实现了
        w.setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint);
        w.show();
        return a.exec();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "signup.h"
    #include 
    
    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();
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90

    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,"注册认证","两次密码输入不一致");
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    页面UI

    mainwindow.ui

    
    <ui version="4.0">
     <class>MainWindowclass>
     <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
       <rect>
        <x>0x>
        <y>0y>
        <width>780width>
        <height>520height>
       rect>
      property>
      <property name="windowTitle">
       <string>MainWindowstring>
      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>385x>
          <y>10y>
          <width>380width>
          <height>500height>
         rect>
        property>
        <property name="text">
         <string/>
        property>
       widget>
       <widget class="QLabel" name="label">
        <property name="geometry">
         <rect>
          <x>10x>
          <y>10y>
          <width>200width>
          <height>20height>
         rect>
        property>
        <property name="text">
         <string>切换页面string>
        property>
       widget>
       <widget class="QLabel" name="label_2">
        <property name="geometry">
         <rect>
          <x>60x>
          <y>80y>
          <width>200width>
          <height>40height>
         rect>
        property>
        <property name="styleSheet">
         <string notr="true">font: 87 20pt "仿宋";string>
        property>
        <property name="text">
         <string>现在登录string>
        property>
       widget>
       <widget class="QPushButton" name="btn_signin">
        <property name="geometry">
         <rect>
          <x>70x>
          <y>400y>
          <width>80width>
          <height>24height>
         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 "Candara";string>
        property>
        <property name="text">
         <string>登陆string>
        property>
       widget>
       <widget class="QPushButton" name="btn_signup">
        <property name="geometry">
         <rect>
          <x>210x>
          <y>400y>
          <width>80width>
          <height>24height>
         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 "Candara";string>
        property>
        <property name="text">
         <string>注册string>
        property>
       widget>
       <widget class="QLineEdit" name="lineEdit_username">
        <property name="geometry">
         <rect>
          <x>60x>
          <y>230y>
          <width>240width>
          <height>40height>
         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 "Candara";string>
        property>
        <property name="placeholderText">
         <string>输入账号string>
        property>
       widget>
       <widget class="QLineEdit" name="lineEdit_password">
        <property name="geometry">
         <rect>
          <x>60x>
          <y>300y>
          <width>240width>
          <height>40height>
         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 "Candara";string>
        property>
        <property name="placeholderText">
         <string>输入密码string>
        property>
       widget>
       <widget class="QPushButton" name="btn_close">
        <property name="geometry">
         <rect>
          <x>725x>
          <y>15y>
          <width>36width>
          <height>36height>
         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>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168

    signup.ui

    
    <ui version="4.0">
     <class>Signupclass>
     <widget class="QWidget" name="Signup">
      <property name="geometry">
       <rect>
        <x>0x>
        <y>0y>
        <width>790width>
        <height>520height>
       rect>
      property>
      <property name="windowTitle">
       <string>Formstring>
      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>15x>
         <y>15y>
         <width>380width>
         <height>490height>
        rect>
       property>
       <property name="text">
        <string/>
       property>
      widget>
      <widget class="QLabel" name="label_2">
       <property name="geometry">
        <rect>
         <x>470x>
         <y>20y>
         <width>131width>
         <height>31height>
        rect>
       property>
       <property name="styleSheet">
        <string notr="true">font: 87 20pt "Arial Black";string>
       property>
       <property name="text">
        <string>你好!string>
       property>
      widget>
      <widget class="QLabel" name="label_3">
       <property name="geometry">
        <rect>
         <x>470x>
         <y>60y>
         <width>261width>
         <height>41height>
        rect>
       property>
       <property name="styleSheet">
        <string notr="true">font: 87 20pt "Arial Black";string>
       property>
       <property name="text">
        <string>欢迎加入我们string>
       property>
      widget>
      <widget class="QLabel" name="label_4">
       <property name="geometry">
        <rect>
         <x>470x>
         <y>140y>
         <width>131width>
         <height>16height>
        rect>
       property>
       <property name="styleSheet">
        <string notr="true">font: 10pt "Arial";string>
       property>
       <property name="text">
        <string>用户名:string>
       property>
      widget>
      <widget class="QLabel" name="label_5">
       <property name="geometry">
        <rect>
         <x>470x>
         <y>200y>
         <width>131width>
         <height>16height>
        rect>
       property>
       <property name="styleSheet">
        <string notr="true">font: 10pt "Arial";string>
       property>
       <property name="text">
        <string>密码:string>
       property>
      widget>
      <widget class="QLabel" name="label_6">
       <property name="geometry">
        <rect>
         <x>470x>
         <y>260y>
         <width>131width>
         <height>16height>
        rect>
       property>
       <property name="styleSheet">
        <string notr="true">font: 10pt "Arial";string>
       property>
       <property name="text">
        <string>再次输入密码:string>
       property>
      widget>
      <widget class="QLineEdit" name="lineEdit_username">
       <property name="geometry">
        <rect>
         <x>470x>
         <y>160y>
         <width>200width>
         <height>30height>
        rect>
       property>
      widget>
      <widget class="QLineEdit" name="lineEdit_passwd">
       <property name="geometry">
        <rect>
         <x>470x>
         <y>220y>
         <width>200width>
         <height>30height>
        rect>
       property>
      widget>
      <widget class="QLineEdit" name="lineEdit_surepasswd">
       <property name="geometry">
        <rect>
         <x>470x>
         <y>290y>
         <width>200width>
         <height>30height>
        rect>
       property>
      widget>
      <widget class="QPushButton" name="btn_sure">
       <property name="geometry">
        <rect>
         <x>470x>
         <y>350y>
         <width>111width>
         <height>24height>
        rect>
       property>
       <property name="styleSheet">
        <string notr="true">background-color: rgb(29, 123, 255);
    color: rgb(255, 255, 255);
    font: 25 9pt "Bahnschrift Light";string>
       property>
       <property name="text">
        <string>确定string>
       property>
      widget>
      <widget class="QPushButton" name="btn_return">
       <property name="geometry">
        <rect>
         <x>470x>
         <y>400y>
         <width>111width>
         <height>24height>
        rect>
       property>
       <property name="styleSheet">
        <string notr="true">background-color: rgb(29, 123, 255);
    color: rgb(255, 255, 255);
    font: 25 9pt "Bahnschrift Light";string>
       property>
       <property name="text">
        <string>返回登录string>
       property>
      widget>
     widget>
     <resources/>
     <connections/>
    ui>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182

    头文件

    mainwindow.h
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include 
    
    #include 
    #include 
    #include 
    #include 
    
    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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    signup.h

    #ifndef SIGNUP_H
    #define SIGNUP_H
    
    #include 
    
    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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    项目文件 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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    图片资源

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

  • 相关阅读:
    PHP将数组导出为CSV文件
    C++知识点总结(23):高级模拟算法真题 ★★★★☆《遥控汽车》《乒乓球比赛》
    内卷时代!程序员如何突破35岁的宿命?
    Pinctrl子系统_03_Pinctrl子系统使用示例
    Linux——线程池
    狂砸40亿美元,亚马逊向OpenAI竞争对手Anthropic投资
    Go 语言学习总结(10)—— Go 初学者必知的十个命令
    【RocketMq 系列】RocketMq 入门教程
    PHP 安装
    外汇天眼:澳大利亚拟规范数字资产中介与交易所!
  • 原文地址:https://blog.csdn.net/gixome/article/details/133993314