• QT用户登录注册,数据库实现


    登录窗口头文件

    #ifndef LOGINUI_H
    #define LOGINUI_H
    
    #include 
    #include 
    #include 
    #include 
    #include 
    
    #include  //数据库管理类
    #include     //执行sql语句的类
    #include    //数据库记录类
    
    #include 
    
    #include "client.h"
    
    class LoginUI : public QWidget
    {
        Q_OBJECT
    
    private:
        //标签类组件
        QLabel* Background_Lab;
        QLabel* Titile_Lab;
        QLabel* Username_Lab;
        QLabel* Password_Lab;
    
        //按钮类组件
        QPushButton* Close_Btn;
        QPushButton* Hide_Btn;
        QPushButton* Login_Btn;
        QPushButton* Register_Btn;
    
        //行编辑器类组件
        QLineEdit* Username_Edit;
        QLineEdit* Password_Edit;
    
        //声明数据库对象
        QSqlDatabase db;
    
        //UI绘制函数
        bool DrawLoginUI();
    
        //构造函数
        LoginUI();
        bool construct();
    
    private slots:
        //槽函数
        void On_CloseBtn_Clicked();
        void On_Loginbtn_Slots();
        void On_Registerbtn_Slots();
    
    
    public:
        //实例化对象
        static LoginUI* NewInstance();
        void show();
        ~LoginUI();
    
    signals:
        void jump();
    
    
    
    };
    #endif //LOGINUI_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
    • 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

    登录窗口函数实现

    #include "LoginUI.h"
    #include 
    
    
    bool LoginUI::DrawLoginUI()
    {
        bool ret = true;
    
        //背景色设置
        Background_Lab = new QLabel(this);
    
        if( Background_Lab != nullptr)
        {
            Background_Lab->resize(538, 160);
            Background_Lab->setStyleSheet("background-color:rgb(115,70,240)");
        }
        else
        {
            ret = false;
        }
    
        //窗口标识
        Titile_Lab = new QLabel(this);
    
        if( Titile_Lab != nullptr )
        {
            Titile_Lab->resize(90, 50);
            Titile_Lab->setPixmap(QPixmap(":/QT_Icon/QQ_Icon.png"));
            Titile_Lab->setScaledContents(true);
        }
        else
        {
            ret = false;
        }
    
        //关闭按键
        Close_Btn = new QPushButton(this);
    
        if( Close_Btn != nullptr )
        {
            Close_Btn->resize(20, 20);
            Close_Btn->setIcon(QIcon(":/QT_Icon/close02.png"));
            Close_Btn->move(508, 10);
            connect(this->Close_Btn, &QPushButton::clicked, this, &LoginUI::On_CloseBtn_Clicked);
        }
        else
        {
            ret = false;
        }
    
        //隐藏按键
        Hide_Btn = new QPushButton(this);
    
        if( Hide_Btn != nullptr )
        {
            Hide_Btn->resize(20, 20);
            Hide_Btn->setIcon(QIcon(":/QT_Icon/hide02.png"));
            Hide_Btn->move(468, 10);
            connect(this->Hide_Btn, &QPushButton::clicked, this, &LoginUI::hide);
        }
        else
        {
            ret = false;
        }
    
        //用户名输入栏
        Username_Edit = new QLineEdit(this);
    
        if( Username_Edit != nullptr )
        {
            Username_Edit->resize(260, 40);
            Username_Edit->move(159, 173);
            Username_Edit->setMaxLength(16);
            Username_Edit->setPlaceholderText("QQ账号/手机号/邮箱");
        }
        else
        {
            ret = false;
        }
    
        //密码输入栏
        Password_Edit = new QLineEdit(this);
    
        if( Password_Edit != nullptr )
        {
            Password_Edit->resize(260, 40);
            Password_Edit->move(Username_Edit->x(), Username_Edit->y()+60);
            Password_Edit->setEchoMode(QLineEdit::Password);
            Password_Edit->setMaxLength(16);
        }
        else
        {
            ret = false;
        }
    
        //用户名输入栏标签
        Username_Lab = new QLabel(this);
    
        if( Username_Lab != nullptr )
        {
            Username_Lab->resize(30, 30);
            Username_Lab->move(Username_Edit->x()-40, Username_Edit->y()+5);
            Username_Lab->setPixmap(QPixmap(":/QT_Icon/windos_icon1.png"));
            Username_Lab->setScaledContents(true);
        }
        else
        {
            ret = false;
        }
    
        //密码输入栏标签
        Password_Lab = new QLabel(this);
    
        if( Password_Lab != nullptr )
        {
            Password_Lab->resize(30, 30);
            Password_Lab->move(Password_Edit->x()-40, Password_Edit->y()+5);
            Password_Lab->setPixmap(QPixmap(":/QT_Icon/password.png"));
            Password_Lab->setScaledContents(true);
        }
        else
        {
            ret = false;
        }
    
        //登录按键
        Login_Btn = new QPushButton("登录",this);
    
        if( Login_Btn != nullptr )
        {
            Login_Btn->resize(130, 40);
            Login_Btn->setStyleSheet("background-color:rgb(8,189,253);");
            Login_Btn->move(Password_Lab->x(), Password_Lab->y()+60);
            connect(this->Login_Btn, &QPushButton::clicked, this, &LoginUI::On_Loginbtn_Slots);
        }
        else
        {
            ret = false;
        }
    
        //注册按键
        Register_Btn = new QPushButton("注册",this);
    
        if( Register_Btn != nullptr )
        {
            Register_Btn->resize(130, 40);
            Register_Btn->setStyleSheet("background-color:rgb(8,189,253);");
            Register_Btn->move(Login_Btn->x()+170, Login_Btn->y());
            connect(this->Register_Btn, &QPushButton::clicked, this, &LoginUI::On_Registerbtn_Slots);
        }
        else
        {
            ret = false;
        }
    
        return ret;
    }
    
    LoginUI::LoginUI(): QWidget(nullptr, Qt::WindowCloseButtonHint)
    {
    }
    
    
    
    void LoginUI::show()
    {
        setWindowFlag(Qt::FramelessWindowHint);
    
        setFixedSize(538, 373);
    
        QWidget::show();
    }
    
    
    LoginUI::~LoginUI()
    {
    }
    
    //构造函数
    bool LoginUI::construct()
    {
    
        //UI界面绘制
        bool ret = true;
    
        ret = DrawLoginUI();
    
    
        //数据库创建
        //判断自己的数据库对象中,是否包含要处理的数据库,没有则添加一个数据库
        if( !db.contains("UserMsg.db"))
        {
            //添加一个数据库,调用该类中的静态成员函数addDatabase()
            db = QSqlDatabase::addDatabase("QSQLITE");//使用sqlite3数据库
    
            //设置数据库名字
            db.setDatabaseName("UserMsg.db");
        }
    
        //此时数据库已经创建完成
        //打开数据库
        if( !db.open() )
        {
            QMessageBox::information(this, "提示", "数据库打开失败");
            ret = false;
        }
    
        //使用sql语句进行创建表的操作
        //准备sql语句
        QString Create_Table_Sql = "create table if not exists user_msg("
                                   "user_name varchar(16) primary key,"
                                   "password varchar(16))";
    
    
        //准备语句执行者
        QSqlQuery querry;
    
        //执行sql语句
        if( !querry.exec(Create_Table_Sql) )
        {
            QMessageBox::information(this, "提示", "创建表失败");
            ret = false;
        }
    
        return ret;
    
    }
    
    //关闭按键槽函数
    void LoginUI::On_CloseBtn_Clicked()
    {
        QMessageBox box(QMessageBox::Question, "关闭", "您确定要退出?", QMessageBox::Yes | QMessageBox::No, this);
        box.setDefaultButton(QMessageBox::No);
    
        int ret = box.exec();
    
        if(ret == QMessageBox::Yes)
        {
            this->close();
        }
        else if(ret == QMessageBox::No)
        {
    
        }
    }
    
    //登录按键槽函数
    void LoginUI::On_Loginbtn_Slots()
    {
        //获取ui界面的用户信息
        QString name = Username_Edit->text();
        QString password = Password_Edit->text();
    
        if( name.isEmpty() )
        {
            QMessageBox::information(this, "提示", "请输入用户名");
            return;
        }
        else if( password.isEmpty() )
        {
            QMessageBox::information(this, "提示", "请输入密码");
            return;
        }
    
        //准备sql查询语句
        QString sql = QString("select * from user_msg where user_name = '%1' and password = '%2'").arg(name).arg(password);
    
        //准备语句执行者
        QSqlQuery querry;
        querry.exec(sql);
    
    //    qDebug() <
    
    //    qDebug() << querry.value(0);
    //    qDebug() << querry.value(1);
    
        //执行sql语句
        if( !querry.next() )
        {
            QMessageBox::information(this, "提示", "账号密码错误");
            return;
        }
        else
        {
    
            QMessageBox::information(this, "提示", "登陆成功");
            emit jump();
            this->close();
    
        }
    
    }
    
    //注册事件槽函数
    void LoginUI::On_Registerbtn_Slots()
    {
        //获取ui界面的用户信息
        QString name = Username_Edit->text();
        QString password = Password_Edit->text();
    
        //确保用户信息输入完整
        if( name.isEmpty() || password.isEmpty() )
        {
            QMessageBox::information(this, "提示", "请将信息填写完整");
            return;
        }
    
        //准备sql语句
        QString sql = QString("insert into user_msg(user_name, password) "
                              "values('%1', '%2')").arg(name).arg(password);
    
        //准备语句执行者
        QSqlQuery querry;
        if( !querry.exec(sql) )
        {
            QMessageBox::information(this, "提示", "注册失败");
            return;
        }
        else
        {
            QMessageBox::information(this, "提示", "注册成功");
        }
    }
    
    
    //创建一个新对象
    LoginUI *LoginUI::NewInstance()
    {
        LoginUI* ret = new LoginUI;
    
        if( !ret->construct() || ret == nullptr )
        {
            delete ret;
    
            ret = nullptr;
        }
    
    
        return ret;
    }
    
    
    • 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
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341

    在这里插入图片描述

  • 相关阅读:
    Java21 + SpringBoot3整合Redis,使用Lettuce连接池,推荐连接池参数配置,封装Redis操作
    机器学习笔记之线性回归——从概率密度函数角度认识最小二乘法
    HR 必知的 360 评估
    React18原理: React核心对象之Update、UpdateQueue、Hook、Task对象
    GGHIGHLIGHT: EASY WAY TO HIGHLIGHT A GGPLOT IN R
    vue3.0+ts+element ui中如何使用svg图片
    基于动态资源使用策略的SMT执行端口侧信道安全防护
    【笔记】《C++性能优化指南》Ch3 测量性能
    JavaWeb_第5章_JSP
    37-Jenkins-多分支流水线
  • 原文地址:https://blog.csdn.net/m0_72847002/article/details/133150715