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

- 日理万妓 2024-02-21 11:13 114阅读 0赞

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

案例截图

" class="reference-link">登录页面登录页面

" class="reference-link">注册页面注册页面

项目目录结构截图

目录结构

代码

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

mainwindow.cpp

  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include "signup.h"
  4. #include <QGraphicsDropShadowEffect>
  5. MainWindow::MainWindow(QWidget *parent)
  6. : QMainWindow(parent)
  7. , ui(new Ui::MainWindow)
  8. {
  9. ui->setupUi(this);
  10. //设置图片
  11. QPixmap *pix = new QPixmap(":/images/blue.png");
  12. QSize sz = ui->label_image->size();
  13. ui->label_image->setPixmap(pix->scaled(sz));
  14. //设置图片阴影效果
  15. QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
  16. shadow->setOffset(-3, 0);
  17. shadow->setColor(QColor(136,136,136));
  18. shadow->setBlurRadius(30);
  19. ui->label_image->setGraphicsEffect(shadow);
  20. }
  21. MainWindow::~MainWindow()
  22. {
  23. delete ui;
  24. }
  25. void sqlite_Init()
  26. {
  27. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
  28. db.setDatabaseName("user.db");
  29. if(!db.open())
  30. {
  31. qDebug()<<"open error";
  32. }
  33. //create excle
  34. QString createsql=QString("create table if not exists user(id integer primary key autoincrement,"
  35. "username ntext unique not NULL,"
  36. "password ntext not NULL)");
  37. QSqlQuery query;
  38. if(!query.exec(createsql)){
  39. qDebug()<<"table create error";
  40. }
  41. else{
  42. qDebug()<<"table create success";
  43. }
  44. }
  45. void MainWindow::on_btn_signin_clicked()
  46. {
  47. sqlite_Init();
  48. QString username = ui->lineEdit_username->text();
  49. QString password = ui->lineEdit_password->text();
  50. QString sql=QString("select * from user where username='%1' and password='%2'").arg(username,password);
  51. //创建执行语句对象
  52. QSqlQuery query(sql);
  53. //判断执行结果
  54. if(!query.next())
  55. {
  56. qDebug()<<"Login error";
  57. QMessageBox::information(this,"登录认证","登录失败,账户或者密码错误");
  58. }
  59. else
  60. {
  61. qDebug()<<"Login success";
  62. QMessageBox::information(this,"登录认证","登录成功");
  63. //登录成功后可以跳转到其他页面
  64. QWidget *w = new QWidget;
  65. w->show();
  66. this->close();
  67. }
  68. }
  69. void MainWindow::on_btn_signup_clicked()
  70. {
  71. this->close();
  72. Signup *s = new Signup;
  73. s->show();
  74. }
  75. void MainWindow::on_btn_close_clicked()
  76. {
  77. this->close();
  78. }

signup.cpp

  1. #include "signup.h"
  2. #include "ui_signup.h"
  3. #include "mainwindow.h"
  4. Signup::Signup(QWidget *parent) :
  5. QWidget(parent),
  6. ui(new Ui::Signup)
  7. {
  8. ui->setupUi(this);
  9. //设置图片
  10. QPixmap *pix = new QPixmap(":/images/women.png");
  11. QSize sz = ui->label_image->size();
  12. ui->label_image->setPixmap(pix->scaled(sz));
  13. }
  14. Signup::~Signup()
  15. {
  16. delete ui;
  17. }
  18. void Signup::on_btn_return_clicked()
  19. {
  20. MainWindow *w = new MainWindow;
  21. w->show();
  22. this->close();
  23. }
  24. void Signup::on_btn_sure_clicked()
  25. {
  26. sqlite_Init();
  27. QString username = ui->lineEdit_username->text();
  28. QString password = ui->lineEdit_passwd->text();
  29. QString surepass = ui->lineEdit_surepasswd->text();
  30. //判断密码是否一致
  31. if(password == surepass)
  32. {
  33. QString sql=QString("insert into user(username,password) values('%1','%2');")
  34. .arg(username).arg(password);
  35. //创建执行语句对象
  36. QSqlQuery query;
  37. //判断执行结果
  38. if(!query.exec(sql))
  39. {
  40. qDebug()<<"insert into error";
  41. QMessageBox::information(this,"注册认证","插入失败!");
  42. }
  43. else
  44. {
  45. qDebug()<<"insert into success";
  46. QMessageBox::information(this,"注册认证","插入成功!");
  47. MainWindow *w = new MainWindow;
  48. w->show();
  49. this->close();
  50. }
  51. }else{
  52. QMessageBox::information(this,"注册认证","两次密码输入不一致");
  53. }
  54. }

页面UI

mainwindow.ui

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

signup.ui

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

头文件

mainwindow.h
  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3. #include <QMainWindow>
  4. #include <QSqlDatabase>
  5. #include <QSqlQuery>
  6. #include <QMessageBox>
  7. #include <QDebug>
  8. void sqlite_Init();
  9. QT_BEGIN_NAMESPACE
  10. namespace Ui {
  11. class MainWindow; }
  12. QT_END_NAMESPACE
  13. class MainWindow : public QMainWindow
  14. {
  15. Q_OBJECT
  16. public:
  17. MainWindow(QWidget *parent = nullptr);
  18. ~MainWindow();
  19. private slots:
  20. void on_btn_signin_clicked();
  21. void on_btn_signup_clicked();
  22. void on_btn_close_clicked();
  23. private:
  24. Ui::MainWindow *ui;
  25. };
  26. #endif // MAINWINDOW_H

signup.h

  1. #ifndef SIGNUP_H
  2. #define SIGNUP_H
  3. #include <QWidget>
  4. namespace Ui {
  5. class Signup;
  6. }
  7. class Signup : public QWidget
  8. {
  9. Q_OBJECT
  10. public:
  11. explicit Signup(QWidget *parent = nullptr);
  12. ~Signup();
  13. private slots:
  14. void on_btn_return_clicked();
  15. void on_btn_sure_clicked();
  16. private:
  17. Ui::Signup *ui;
  18. };
  19. #endif // SIGNUP_H

项目文件 Login.pro

  1. QT += core gui sql
  2. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  3. CONFIG += c++17
  4. # You can make your code fail to compile if it uses deprecated APIs.
  5. # In order to do so, uncomment the following line.
  6. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
  7. SOURCES += \
  8. main.cpp \
  9. mainwindow.cpp \
  10. signup.cpp
  11. HEADERS += \
  12. mainwindow.h \
  13. signup.h
  14. FORMS += \
  15. mainwindow.ui \
  16. signup.ui
  17. # Default rules for deployment.
  18. qnx: target.path = /tmp/$${TARGET}/bin
  19. else: unix:!android: target.path = /opt/$${TARGET}/bin
  20. !isEmpty(target.path): INSTALLS += target
  21. RESOURCES += \
  22. images.qrc

图片资源

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

发表评论

表情:
评论列表 (有 0 条评论,114人围观)

还没有评论,来说两句吧...

相关阅读

    相关 qt sqlite数据库操作

    \\\\\Qt 提供了与平台以及数据库种类无关的访问SQL数据库的接口, QSqlDatabase对象象征了数据库的关联。下面就qt 操作sqlite3数据库做总结。\\\\\