• QT实现qq登录


    1、登录界面

    头文件

    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3. #include
    4. #include
    5. #include
    6. #include "second.h" //第二个界面头文件
    7. #include "third.h" //注册界面头文件
    8. #include //数据库管理类
    9. #include //执行sql语句的类
    10. #include //数据库记录的类
    11. #include
    12. QT_BEGIN_NAMESPACE
    13. namespace Ui { class MainWindow; }
    14. QT_END_NAMESPACE
    15. class MainWindow : public QMainWindow
    16. {
    17. Q_OBJECT
    18. public:
    19. MainWindow(QWidget *parent = nullptr);
    20. ~MainWindow();
    21. signals:
    22. void jump(); //自定义跳转信号函数
    23. void jump2(); //自定义跳转信号函数
    24. public:
    25. void jump_slot3(); //自定义跳转函数(注册页面)
    26. private slots:
    27. void on_pushButton_clicked();
    28. void on_pushButton_2_clicked();
    29. void on_pushButton_3_clicked();
    30. private:
    31. Ui::MainWindow *ui;
    32. Second *s1; //定义另一个界面的指针
    33. third *t1; //定义注册页面的指针
    34. QSqlDatabase db; //定义一个数据库的类
    35. };
    36. #endif // MAINWINDOW_H

    源文件

    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. MainWindow::MainWindow(QWidget *parent)
    4. : QMainWindow(parent)
    5. , ui(new Ui::MainWindow)
    6. {
    7. ui->setupUi(this);
    8. ui->label->setPixmap(QPixmap("D:\\QTtupian\\jimoaqing.gif"));
    9. ui->label->setScaledContents(true);
    10. //账号图标
    11. ui->label_2->setPixmap(QPixmap("D:\\QTtupian\\zhanghao.jpg"));
    12. ui->label_2->setScaledContents(true);
    13. //密码图标
    14. ui->label_3->setPixmap(QPixmap("D:\\QTtupian\\mima.jpg"));
    15. ui->label_3->setScaledContents(true);
    16. s1 = new Second; //给另一个界面实例化空间
    17. t1 = new third; //给注册页面实例化空间
    18. //将当前界面的信号,与s1界面的槽函数进行连接
    19. connect(this,&MainWindow::jump, s1, &Second::jump_slot); //连接聊天室
    20. connect(this,&MainWindow::jump2, t1, &third::jump_slot2); //连接注册
    21. connect(t1,&third::jump3, this, &MainWindow::jump_slot3); //从注册跳回
    22. /********************************************************/
    23. //判断自己的数据库对象中,是否包含了要处理的数据库
    24. if(!db.contains("zhanghu.db"))
    25. {
    26. db = QSqlDatabase::addDatabase("QSQLITE"); //参数为驱动类型
    27. //设置数据库的名字
    28. db.setDatabaseName("zhanghu.db");
    29. }
    30. //此时已经有一个名为zhanghu.db的数据库
    31. //打开数据库
    32. if(!db.open())
    33. {
    34. QMessageBox::information(this,"失败","数据库打开失败");
    35. return ;
    36. }
    37. //zhanghu_info
    38. }
    39. MainWindow::~MainWindow()
    40. {
    41. delete ui;
    42. }
    43. //注册按钮对应槽函数
    44. void MainWindow::on_pushButton_3_clicked()
    45. {
    46. emit jump2(); //发送跳转函数
    47. this->hide(); //将当前页面隐藏
    48. }
    49. //自定义跳转函数
    50. void MainWindow::jump_slot3()
    51. {
    52. this->show();
    53. }
    54. //登录按钮对应槽函数
    55. void MainWindow::on_pushButton_clicked()
    56. {
    57. //准备sql语句
    58. QString sql = "select * from zhanghu_info";
    59. //准备语句执行者
    60. QSqlQuery querry;
    61. //执行sql语句
    62. if(!querry.exec(sql))
    63. {
    64. QMessageBox::information(this,"提示","显示失败");
    65. return ;
    66. }
    67. int i=0; //记录行数
    68. QString Z; //记录账号
    69. QString M; //记录密码
    70. while(querry.next())
    71. {
    72. //遍历每条记录中的每一项内容
    73. for(int j=0;j<querry.record().count();j++) //其中count为每一行数据的个数
    74. {
    75. if(ui->lineEdit->text()==querry.record().value(j).toString()&&
    76. ui->lineEdit_2->text()==querry.record().value(j+1).toString())
    77. {
    78. Z = querry.record().value(j).toString();
    79. M = querry.record().value(j+1).toString();
    80. }
    81. }
    82. i++;
    83. }
    84. //判断输入的账号密码是否与数据库表中已注册的账号密码一致
    85. if(ui->lineEdit->text()==Z&&ui->lineEdit_2->text()==M)
    86. {
    87. //信息对话框
    88. QMessageBox box(QMessageBox::Question,"成功","登录成功",
    89. QMessageBox::Ok,this);
    90. box.setDefaultButton(QMessageBox::Ok); //默认到ok上
    91. int ret = box.exec(); //运行对话框
    92. if(ret == QMessageBox::Ok)
    93. {
    94. emit jump(); //发送跳转信息
    95. this->hide(); //将当前界面隐藏
    96. }
    97. }
    98. else
    99. {
    100. //错误对话框
    101. QMessageBox box(QMessageBox::Question,"错误","账号密码不匹配,是否重新登录",
    102. QMessageBox::Ok|QMessageBox::Cancel,this);
    103. box.setDefaultButton(QMessageBox::Ok); //默认到ok上
    104. int ret = box.exec(); //运行对话框
    105. switch(ret)
    106. {
    107. case QMessageBox::Ok:
    108. ui->lineEdit->clear(); //清楚账号密码内容
    109. ui->lineEdit_2->clear();
    110. break;
    111. case QMessageBox::Cancel:
    112. //ret=box.close();
    113. break;
    114. }
    115. }
    116. }
    117. //退出按钮对应槽函数
    118. void MainWindow::on_pushButton_2_clicked()
    119. {
    120. //问题对话框
    121. int ret = QMessageBox::question(this,"问题","是否确定要退出登录",
    122. QMessageBox::Yes|QMessageBox::No,
    123. QMessageBox::No);
    124. switch(ret)
    125. {
    126. case QMessageBox::Yes:
    127. this->close(); //关闭窗口
    128. break;
    129. case QMessageBox::No:
    130. break;
    131. }
    132. }

    主函数

    1. #include "mainwindow.h"
    2. #include
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6. MainWindow w;
    7. w.show();
    8. return a.exec();
    9. }

    2、注册窗口

    头文件

    1. #ifndef THIRD_H
    2. #define THIRD_H
    3. #include
    4. #include
    5. #include
    6. #include //数据库管理类
    7. #include //执行sql语句的类
    8. #include //数据库记录的类
    9. #include
    10. namespace Ui {
    11. class third;
    12. }
    13. class third : public QDialog
    14. {
    15. Q_OBJECT
    16. public:
    17. explicit third(QWidget *parent = nullptr);
    18. ~third();
    19. signals:
    20. void jump3(); //自定义跳转信号函数
    21. public:
    22. void jump_slot2(); //接收跳转信号的槽函数
    23. private slots:
    24. void on_pushButton_clicked();
    25. void on_pushButton_2_clicked();
    26. private:
    27. Ui::third *ui;
    28. QSqlDatabase db;
    29. };
    30. #endif // THIRD_H

    源文件

    1. #include "third.h"
    2. #include "ui_third.h"
    3. third::third(QWidget *parent) :
    4. QDialog(parent),
    5. ui(new Ui::third)
    6. {
    7. ui->setupUi(this);
    8. ui->label->setPixmap(QPixmap("D:\\QTtupian\\jimoaqing.gif"));
    9. ui->label->setScaledContents(true);
    10. //账号图标
    11. ui->label_2->setPixmap(QPixmap("D:\\QTtupian\\zhanghao.jpg"));
    12. ui->label_2->setScaledContents(true);
    13. //密码图标
    14. ui->label_3->setPixmap(QPixmap("D:\\QTtupian\\mima.jpg"));
    15. ui->label_3->setScaledContents(true);
    16. /**********************************************************/
    17. //判断自己的数据库对象中,是否包含了要处理的数据库
    18. if(!db.contains("zhanghu.db"))
    19. {
    20. db = QSqlDatabase::addDatabase("QSQLITE"); //参数为驱动类型
    21. //设置数据库的名字
    22. db.setDatabaseName("zhanghu.db");
    23. }
    24. //此时已经有一个名为zhanghu.db的数据库
    25. //打开数据库
    26. if(!db.open())
    27. {
    28. QMessageBox::information(this,"失败","数据库打开失败");
    29. return ;
    30. }
    31. //创建一个表
    32. //准备sql语句
    33. QString sql="create table if not exists zhanghu_info("
    34. "zhanghao varchar(30),mima varchar(30))";
    35. //准备语句执行者
    36. QSqlQuery querry;
    37. //执行者执行sql语句
    38. if(!querry.exec(sql)) //注意要把sql语句放进函数中
    39. {
    40. QMessageBox::information(this,"失败","创建表失败");
    41. return ;
    42. }
    43. }
    44. third::~third()
    45. {
    46. delete ui;
    47. }
    48. //接收跳转信号对应的槽函数
    49. void third::jump_slot2()
    50. {
    51. this->show(); //将自己界面进行展示
    52. }
    53. //账户注册槽函数
    54. void third::on_pushButton_clicked()
    55. {
    56. //获取ui界面输入的账号和密码
    57. QString zhanghao = ui->lineEdit->text();
    58. QString mima = ui->lineEdit_2->text();
    59. //确保每个编辑器都有数据
    60. if(zhanghao.isEmpty()||mima.isEmpty())
    61. {
    62. QMessageBox::information(this,"提示","请将信息填写完整");
    63. return ;
    64. }
    65. //准备sql语句
    66. QString sql = "select * from zhanghu_info";
    67. //准备语句执行者
    68. QSqlQuery querry;
    69. //执行sql语句
    70. if(!querry.exec(sql))
    71. {
    72. QMessageBox::information(this,"提示","显示失败");
    73. return ;
    74. }
    75. int i=0; //记录行数
    76. QString Z; //记录账号
    77. QString M; //记录密码
    78. while(querry.next())
    79. {
    80. //遍历每条记录中的每一项内容
    81. for(int j=0;j<querry.record().count();j++) //其中count为每一行数据的个数
    82. {
    83. if(ui->lineEdit->text()==querry.record().value(j).toString())
    84. {
    85. QMessageBox::information(this,"失败","该账号已被注册请重新输入");
    86. return ;
    87. }
    88. }
    89. i++;
    90. }
    91. //准备sql语句
    92. QString sql2 = QString("insert into zhanghu_info(zhanghao,mima)"
    93. "values('%1',\"%2\")").arg(zhanghao).arg(mima);
    94. //准备语句执行者
    95. if(!querry.exec(sql2))
    96. {
    97. QMessageBox::information(this,"失败","注册失败");
    98. return ;
    99. }
    100. else
    101. {
    102. QMessageBox::information(this,"成功","注册成功");
    103. }
    104. }
    105. void third::on_pushButton_2_clicked()
    106. {
    107. emit jump3();
    108. this->hide();
    109. }

    3、登录后界面

    头文件

    1. #ifndef SECOND_H
    2. #define SECOND_H
    3. #include
    4. namespace Ui {
    5. class Second;
    6. }
    7. class Second : public QDialog
    8. {
    9. Q_OBJECT
    10. public:
    11. explicit Second(QWidget *parent = nullptr);
    12. ~Second();
    13. public slots:
    14. void jump_slot(); //接收跳转信号的槽函数
    15. private:
    16. Ui::Second *ui;
    17. };
    18. #endif // SECOND_H

    源文件

    1. #include "second.h"
    2. #include "ui_second.h"
    3. Second::Second(QWidget *parent) :
    4. QDialog(parent),
    5. ui(new Ui::Second)
    6. {
    7. ui->setupUi(this);
    8. }
    9. Second::~Second()
    10. {
    11. delete ui;
    12. }
    13. //接收跳转信号对应的槽函数
    14. void Second::jump_slot()
    15. {
    16. this->show(); //将自己界面进行展示
    17. }

    4、思维导图

  • 相关阅读:
    阿里云服务器安装tomcat
    TCP之三次握手四次挥手
    TNZ5TSC OSN1800V全新板卡100G支路业务处理板
    WebSocket真实项目总结
    【数据结构之栈、队列、数组】
    qt基础之全局静态变量
    【C++】内联函数的原理及使用
    Perl 中的模式匹配修饰符
    ICLR 19 :APPNP + ICML 20 GCNII
    求职刷题力扣DAY14 ---二叉树的基础遍历,迭代、递归
  • 原文地址:https://blog.csdn.net/Venusler/article/details/133151765