• 2023年9月21日


    完善登录界面的注册登录功能

    头文件1

    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. #include
    13. QT_BEGIN_NAMESPACE
    14. namespace Ui {class MainWindow;}
    15. QT_END_NAMESPACE
    16. class MainWindow : public QMainWindow
    17. {
    18. Q_OBJECT
    19. public:
    20. MainWindow(QWidget *parent = nullptr);
    21. ~MainWindow();
    22. public slots:
    23. //void on_btn1_clicked();
    24. void my_slot1();
    25. void my_slot2();
    26. void my_slot3();
    27. private:
    28. Ui::MainWindow *ui;
    29. QLineEdit *edti1;
    30. QLineEdit *edti2;
    31. QPushButton *btn1;
    32. QPushButton *btn2;
    33. QPushButton *btn3;
    34. QLabel *lab1;
    35. QLabel *lab2;
    36. QLabel *lab3;
    37. QSqlDatabase db; //定义一个数据库的类对象
    38. signals:
    39. //void btn1_signal();
    40. void jump();
    41. void jump2();
    42. };
    43. #endif // MAINWINDOW_H

    .cpp1

    1. #include "mainwindow.h"
    2. #include
    3. #include
    4. #include
    5. #include
    6. MainWindow::MainWindow(QWidget *parent)
    7. : QMainWindow(parent)
    8. {
    9. //界面
    10. this->setFixedSize(800,600); //设置固定尺寸
    11. this->setWindowTitle("摇篮游行"); //设置窗口标签
    12. this->setWindowIcon(QIcon(":/pict/123.webp")); //设置窗口图标
    13. //登录按钮
    14. btn1 = new QPushButton(QIcon(":/pict/213.jpg"),"登录",this); //构造一个按钮
    15. btn1->resize(100,50); //设置按钮大小
    16. btn1->move(550,420); //移动按钮
    17. //退出按钮
    18. btn2 = new QPushButton(QIcon(":/pict/12345.jpg"),"退出",this); //构造一个按钮
    19. btn2->resize(btn1->size());
    20. btn2->move(550,520);
    21. //注册按钮
    22. btn3 = new QPushButton(QIcon(":/pict/1234567.jpg"),"注册",this);
    23. btn3->resize(btn1->size());
    24. btn3->move(680,420);
    25. //行编辑器
    26. edti1 = new QLineEdit(this); //构造一个行编辑器
    27. edti1->setPlaceholderText("繁星凝望着海洋");
    28. edti1->resize(300,50);
    29. edti1->move(180,420);
    30. edti2 = new QLineEdit(this); //构造一个行编辑器
    31. edti2->setPlaceholderText("海洋拥抱着风帆");
    32. edti2->resize(300,50);
    33. edti2->move(180,520);
    34. //按钮事件
    35. connect(btn1,SIGNAL(clicked()),this,SLOT(my_slot1()));
    36. connect(btn2,SIGNAL(clicked()),this,SLOT(my_slot2()));
    37. connect(btn3,SIGNAL(clicked()),this,SLOT(my_slot3()));
    38. //标签
    39. lab1 = new QLabel(this);
    40. lab1->resize(80,50);
    41. lab1->setPixmap(QPixmap(":/pict/321.webp"));
    42. lab1->setScaledContents(true);
    43. lab1->move(100,420);
    44. lab2 = new QLabel(this);
    45. lab2->resize(80,50);
    46. lab2->setPixmap(QPixmap(":/pict/4321.webp"));
    47. lab2->setScaledContents(true);
    48. lab2->move(100,520);
    49. //gif
    50. QMovie *movie = new QMovie(":/pict/6.gif");
    51. lab3 = new QLabel(this);
    52. lab3->resize(800,400);
    53. lab3->setMovie(movie);
    54. movie->start();
    55. lab3->setScaledContents(true);
    56. lab3->move(0,0);
    57. //如果没有则创建数据库
    58. if(!db.contains("lamia.db"))
    59. {
    60. //添加一个数据库
    61. db = QSqlDatabase::addDatabase("QSQLITE");
    62. //设置名字
    63. db.setDatabaseName("lamia.db");
    64. }
    65. //打开数据库
    66. if(!db.open())
    67. {
    68. QMessageBox::information(this,"失败","数据库打开失败");
    69. return ;
    70. }
    71. }
    72. //登录按钮
    73. void MainWindow::my_slot1()
    74. {
    75. //sql语句
    76. QString sql = "select * from user";
    77. QSqlQuery querry;
    78. if(!querry.exec(sql))
    79. {
    80. QMessageBox::information(this,"提示","信息查询失败");
    81. return;
    82. }
    83. int flag=0;
    84. while(querry.next())
    85. {
    86. QString user = querry.record().value(0).toString();
    87. QString word = querry.record().value(1).toString();
    88. if(this->edti1->text()==user&&this->edti2->text()==word)
    89. {
    90. QMessageBox::information(this,"登录成功","登录成功!");
    91. emit jump();
    92. flag=1;
    93. this->close();
    94. }
    95. }
    96. if(flag==0)
    97. {
    98. QMessageBox::critical(
    99. this,tr("摇篮游行"),
    100. tr("账号或密码不正确,是否重新登录"),
    101. QMessageBox::Yes | QMessageBox::Cancel,
    102. QMessageBox::Cancel
    103. );
    104. }
    105. }
    106. //退出按钮
    107. void MainWindow::my_slot2()
    108. {
    109. int ret = QMessageBox::question(
    110. this,tr("摇篮游行"),
    111. tr("是否退出"),
    112. QMessageBox::Yes | QMessageBox::No,
    113. QMessageBox::No);
    114. if(ret==QMessageBox::Yes)
    115. {
    116. this->close();
    117. }
    118. }
    119. //注册按钮
    120. void MainWindow::my_slot3()
    121. {
    122. emit jump2();
    123. }
    124. MainWindow::~MainWindow()
    125. {
    126. }

    头文件2

    1. #ifndef SECOND_H
    2. #define SECOND_H
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. #include
    13. #include
    14. namespace Ui {
    15. class second;
    16. }
    17. class second : public QWidget
    18. {
    19. Q_OBJECT
    20. public slots:
    21. void jump_slot();
    22. public:
    23. explicit second(QWidget *parent = nullptr);
    24. ~second();
    25. private:
    26. Ui::second *ui;
    27. QLabel *lab1;
    28. };
    29. #endif // SECOND_H

    .cpp2

    1. #include "second.h"
    2. #include "ui_second.h"
    3. second::second(QWidget *parent) :
    4. QWidget(parent),
    5. ui(new Ui::second)
    6. {
    7. ui->setupUi(this);
    8. this->setFixedSize(800,600); //设置固定尺寸
    9. this->setWindowTitle("摇篮游行"); //设置窗口标签
    10. this->setWindowIcon(QIcon(":/pict/123.webp")); //设置窗口图标
    11. lab1 = new QLabel(this);
    12. lab1->resize(800,600);
    13. lab1->setText("施工中");
    14. lab1->setAlignment(Qt::AlignCenter);
    15. lab1->move(0,0);
    16. }
    17. second::~second()
    18. {
    19. delete ui;
    20. }
    21. void second::jump_slot()
    22. {
    23. this->show();
    24. }

    头文件3

    1. #ifndef THIRD_H
    2. #define THIRD_H
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. #include
    13. namespace Ui {
    14. class third;
    15. }
    16. class third : public QWidget
    17. {
    18. Q_OBJECT
    19. public:
    20. explicit third(QWidget *parent = nullptr);
    21. QLineEdit *edti1;
    22. QLineEdit *edti2;
    23. ~third();
    24. public slots:
    25. void jump_slot();
    26. void my_slot1();
    27. void my_slot2();
    28. private:
    29. //定义ui界面
    30. Ui::third *ui;
    31. QPushButton *btn2;
    32. QPushButton *btn3;
    33. QLabel *lab1;
    34. QSqlDatabase db; //定义一个数据库的类对象
    35. };
    36. #endif // THIRD_H

    .cpp3

    1. #include "third.h"
    2. #include "ui_third.h"
    3. third::third(QWidget *parent) :
    4. QWidget(parent),
    5. ui(new Ui::third)
    6. {
    7. ui->setupUi(this);
    8. //界面
    9. this->setFixedSize(450,400); //设置固定尺寸
    10. this->setWindowTitle("账号注册"); //设置窗口标签
    11. this->setWindowIcon(QIcon(":/pict/1234567.jpg")); //设置窗口图标
    12. //注册按钮
    13. btn3 = new QPushButton(QIcon(":/pict/4321.png"),"注册",this);
    14. btn3->resize(100,50);
    15. btn3->move(300,270);
    16. //退出按钮
    17. btn2 = new QPushButton(QIcon(":/pict/123123.png"),"退出",this);
    18. btn2->resize(100,50);
    19. btn2->move(300,340);
    20. //标签
    21. lab1 = new QLabel(this);
    22. lab1->resize(500,250);
    23. lab1->setPixmap(QPixmap(":/pict/54321.jpg"));
    24. lab1->setScaledContents(false);
    25. lab1->move(0,0);
    26. //账号密码栏
    27. edti1 = new QLineEdit(this); //构造一个行编辑器
    28. edti1->setPlaceholderText("注册账号");
    29. edti1->resize(200,50);
    30. edti1->move(20,270);
    31. edti2 = new QLineEdit(this); //构造一个行编辑器
    32. edti2->setPlaceholderText("注册密码");
    33. edti2->resize(200,50);
    34. edti2->move(20,340);
    35. connect(btn3,SIGNAL(clicked()),this,SLOT(my_slot1()));
    36. connect(btn2,SIGNAL(clicked()),this,SLOT(my_slot2()));
    37. //如果没有则创建数据库
    38. if(!db.contains("lamia.db"))
    39. {
    40. //添加一个数据库
    41. db = QSqlDatabase::addDatabase("QSQLITE");
    42. //设置名字
    43. db.setDatabaseName("lamia.db");
    44. }
    45. //打开数据库
    46. if(!db.open())
    47. {
    48. QMessageBox::information(this,"失败","数据库打开失败");
    49. return ;
    50. }
    51. //创建表
    52. QString sql = "create table if not exists user("
    53. "username varchar(20),"
    54. "password varchar(20))";
    55. QSqlQuery querry;
    56. if(!querry.exec(sql))
    57. {
    58. QMessageBox::information(this,"失败","创建表失败");
    59. return ;
    60. }
    61. }
    62. third::~third()
    63. {
    64. delete ui;
    65. }
    66. void third::jump_slot()
    67. {
    68. this->show();
    69. }
    70. void third::my_slot2()
    71. {
    72. int ret = QMessageBox::question(
    73. this,tr("退出提示"),
    74. tr("是否退出"),
    75. QMessageBox::Yes | QMessageBox::No,
    76. QMessageBox::No);
    77. if(ret==QMessageBox::Yes)
    78. {
    79. this->close();
    80. }
    81. }
    82. void third::my_slot1()
    83. {
    84. //获取输入栏中的内容
    85. QString user = this->edti1->text();
    86. QString password = this->edti2->text();
    87. //确保账号密码都被输入
    88. if(user.isEmpty()||password.isEmpty())
    89. {
    90. QMessageBox::information(this,"提示","请将信息填写完整");
    91. return ;
    92. }
    93. //遍历查看账号是否以注册
    94. //sql语句
    95. QString sql = "select * from user";
    96. QSqlQuery querry;
    97. if(!querry.exec(sql))
    98. {
    99. QMessageBox::information(this,"提示","账号查询失败");
    100. return;
    101. }
    102. int flag=0;
    103. QString olduser="";
    104. while(querry.next())
    105. {
    106. olduser = querry.record().value(0).toString();
    107. if(olduser==user)
    108. {
    109. QMessageBox::warning(this,"错误","改账号已注册请重新输入");
    110. this->edti1->clear();
    111. this->edti2->clear();
    112. flag=1;
    113. }
    114. }
    115. //sql语句录入信息
    116. if(flag==0)
    117. {
    118. sql = QString("insert into user(username,password)"
    119. "values('%1','%2')").arg(user).arg(password);
    120. QSqlQuery querry;
    121. if(!querry.exec(sql))
    122. {
    123. QMessageBox::information(this,"失败","注册失败");
    124. return;
    125. }else
    126. {
    127. QMessageBox::information(this,"成功","注册成功");
    128. this->edti1->clear();
    129. this->edti2->clear();
    130. }
    131. this->close();
    132. }
    133. }

  • 相关阅读:
    安装推送jar包到远程仓库
    【剑指Offer】29.顺时针打印矩阵
    力扣每日一题---2594. 修车的最少时间
    启明欣欣STM32开发板闪烁LED实验
    LeetCode_674_最长连续递增序列
    图划分(Graph Partition&Re-ordering): METIS(5.x)&ParMETIS(4.x)使用实践
    Django性能之道:缓存应用与优化实战
    冥想第五百四十四天
    office办公软件太贵了 Microsoft的Word为什么要买 Microsoft365家庭版多少钱 Microsoft365密钥
    JavaSE17——面向对象_多态
  • 原文地址:https://blog.csdn.net/2201_75732711/article/details/133147910