• QT-day5


    1、添加注册功能到数据库

    头文件

    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include
    4. #include //消息对话框类头文件
    5. #include
    6. #include
    7. #include //数据库管理类
    8. #include //执行sql语句的类
    9. #include //数据库记录的类
    10. #include "second.h"
    11. QT_BEGIN_NAMESPACE
    12. namespace Ui { class Widget; }
    13. QT_END_NAMESPACE
    14. class Widget : public QWidget
    15. {
    16. Q_OBJECT
    17. signals:
    18. void my_signal(); //定义一个无参无返回值的信号函数
    19. void jump(); //自定义跳转信号函数
    20. public:
    21. Widget(QWidget *parent = nullptr);
    22. ~Widget();
    23. private slots:
    24. void my_slot(); //自定义无参无返回值槽函数
    25. void on_dengBtn_clicked();
    26. void on_quBtn_clicked();
    27. void on_zhuBtn_clicked();
    28. private:
    29. Ui::Widget *ui;
    30. second *s1;
    31. QSqlDatabase db; //定义一个数据库的类对象
    32. };
    33. #endif // WIDGET_H

    源文件

    1. #include "widget.h"
    2. #include "ui_widget.h"
    3. Widget::Widget(QWidget *parent)
    4. : QWidget(parent)
    5. , ui(new Ui::Widget)
    6. {
    7. ui->setupUi(this);
    8. s1 = new second;
    9. //使用qt5版本,把登录、注册、取消按钮连接
    10. connect(ui->dengBtn, &QPushButton::clicked, this, &Widget::my_slot);
    11. connect(ui->quBtn, &QPushButton::clicked, this, &Widget::my_slot);
    12. connect(ui->zhuBtn, &QPushButton::clicked, this, &Widget::my_slot);
    13. connect(this, &Widget::jump, s1, &second::jump_slot);
    14. this->setWindowTitle("灵能事务所"); //设置窗口标题
    15. this->setWindowIcon(QIcon(":/pic/biao.jpg"));
    16. //判断自己的数据库对象中是否包含了要处理的数据库,如果没有包含,则添加一个数据库,如果包含了,就可以打开
    17. if(!db.contains("mydatabase.db"))
    18. {
    19. //添加一个数据库,调用该类中的静态成员函数addDatabase
    20. db = QSqlDatabase::addDatabase("QSQLITE");
    21. //设置数据库的名字
    22. db.setDatabaseName("mydatabase.db");
    23. }
    24. //此时已经有一个名为mydatabase.db的数据库
    25. // 打开数据库
    26. if(!db.open())
    27. {
    28. QMessageBox::information(this, "失败", "打开失败");
    29. return;
    30. }
    31. //需要使用sql语句进行创建表的操作
    32. //1、准备sql语句
    33. QString sql = "create table if not exists stu_info(" //创建表
    34. "zczh integer primary key," //注册账号,主键
    35. "zcmm integer)"; //注册密码
    36. //准备语句执行者
    37. QSqlQuery querry;
    38. //让语句执行者执行sql语句
    39. if(!querry.exec(sql))
    40. {
    41. QMessageBox::information(this, "失败", "创建表失败");
    42. return;
    43. }
    44. }
    45. Widget::~Widget()
    46. {
    47. delete ui;
    48. }
    49. void Widget::my_slot()
    50. {
    51. //emit jump();
    52. }
    53. //登录按钮对应的槽函数
    54. void Widget::on_dengBtn_clicked()
    55. {
    56. //传入账号密码
    57. QString zczh = ui->lineEdit1->text();
    58. QString zcmm = ui->lineEdit2->text();
    59. QString sql = QString("select * from stu_info where zczh = '%1' and zcmm = '%2' ").arg(zczh).arg(zcmm);
    60. //准备语句执行者
    61. QSqlQuery querry;
    62. if(!querry.exec(sql))
    63. {
    64. //1、调用构造函数实例化对象
    65. QMessageBox::information(this, "失败", "登录失败");
    66. //3、对结果进行判断
    67. if(QMessageBox::Ok)
    68. {
    69. ui->lineEdit1->clear();
    70. ui->lineEdit2->clear();
    71. qDebug()<<"继续登录";
    72. }else if(QMessageBox::Cancel)
    73. {
    74. close();
    75. }
    76. }else
    77. {
    78. //1、调用构造函数实例化对象
    79. QMessageBox::information(this, "成功", "登录成功");
    80. //3、对结果进行判断
    81. if(QMessageBox::Ok)
    82. {
    83. emit jump();
    84. }
    85. }
    86. }
    87. //取消
    88. void Widget::on_quBtn_clicked()
    89. {
    90. //1、调用静态函数实例化对象
    91. int ret = QMessageBox::information(this, //父组件
    92. "问题", //对话框标题
    93. "是否确定退出登录", //对话框文本内容
    94. QMessageBox::Yes | QMessageBox::No, //对话框提供的按钮
    95. QMessageBox::No); //默认选中的按钮
    96. //3、对结果进行判断
    97. if(ret == QMessageBox::Yes)
    98. {
    99. close();
    100. }
    101. }
    102. //注册
    103. void Widget::on_zhuBtn_clicked()
    104. {
    105. //获取ui界面中要录入的数据
    106. QString zczh = ui->lineEdit1->text();
    107. QString zcmm = ui->lineEdit2->text();
    108. //要确保每个编辑器中都有数据
    109. if(zczh.isEmpty() || zcmm.isEmpty())
    110. {
    111. QMessageBox::information(this, "提示", "账号或密码为空,请重新输入");
    112. return ;
    113. }
    114. //准备sql语句
    115. QString sql = QString("insert into stu_info(zczh, zcmm)"
    116. "values('%1', '%2')").arg(zczh).arg(zcmm);
    117. //准备语句执行者
    118. QSqlQuery querry;
    119. if(!querry.exec(sql))
    120. {
    121. QMessageBox::information(this, "失败", "注册失败");
    122. return;
    123. }else
    124. {
    125. QMessageBox::information(this, "成功", "注册成功");
    126. }
    127. }

    ui界面

  • 相关阅读:
    idea导入springboot项目运行教程
    9. 回文数 --力扣 --JAVA
    找单身狗。一个数组中只有两个数字出现一次,其他数字出现了两次,编写一个函数找出这两个只出现一次的数字
    架构之路15. 创业 - 厌倦
    Docker教程
    十三、vite项目中无法使用minio的解决方案
    Python与数据分析--每天绘制Matplotlib库实例图片3张-第1天
    从零学算法(LCR 135)
    二分查找——704. 二分查找
    不降逼格的情况下降价格!Chromebook Plus让创新延续和计算机重生
  • 原文地址:https://blog.csdn.net/Lychee_z23/article/details/133148549