• 9.21号作业


    实现把注册的信息导入数据库中

    second.h

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

    widget.h

    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. QT_BEGIN_NAMESPACE
    13. namespace Ui { class Widget; }
    14. QT_END_NAMESPACE
    15. class Widget : public QWidget
    16. {
    17. Q_OBJECT
    18. public:
    19. Widget(QWidget *parent = nullptr);
    20. ~Widget();
    21. signals:
    22. void mysignals();
    23. public slots:
    24. void userLogin();
    25. void userExit();
    26. void userregister();
    27. private:
    28. QPushButton *btn1;
    29. QPushButton *btn2;
    30. QPushButton *btn3;
    31. QLabel *lab1;
    32. QLabel *lab2;
    33. QLabel *lab3;
    34. QLineEdit *edit1;
    35. QLineEdit *edit2;
    36. private:
    37. Ui::Widget *ui;
    38. QSqlDatabase db;
    39. };
    40. #endif // WIDGET_H

    widget.cpp

    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. //判断自己的数据库对象中,是否包含了要处理的数据库,如果没有包含则添加一个数据库,如果包含了,就可以打开了
    9. if(!db.contains("mydatabase.db"))
    10. {
    11. //添加一个数据库,调用该类中的静态成员函数addDatabase
    12. db = QSqlDatabase::addDatabase("QSQLITE");
    13. //设置数据库的名字
    14. db.setDatabaseName("mydatabase.db");
    15. }
    16. //此时已经有一个名为mydatabase.db的数据库
    17. //打开数据库
    18. if(!db.open())
    19. {
    20. QMessageBox::information(this,"失败","数据库打开失败");
    21. return;
    22. }
    23. //需要使用sql语句进行创建表的操作
    24. //准备sql语句
    25. QString sql = "create table if not exists stu_info(" //创建表
    26. " userName varchar(20) primary key," //账号,主键
    27. "password varchar(20))"; //密码
    28. //准备语句执行者
    29. QSqlQuery querry;
    30. //让语句执行者执行sql语句
    31. //函数原型:bool exec(const QString& query);
    32. //参数:要执行的sql语句
    33. //返回值:成功执行返回true,失败返回false
    34. if(!querry.exec(sql))
    35. {
    36. QMessageBox::information(this, "失败", "创建表失败");
    37. return;
    38. }
    39. //构造一个登录按钮,并指定父组件,图标,和文本内容
    40. btn1=new QPushButton(QIcon("C:\\Users\\wuhuiwu\\Desktop\\login.png"),"登录",this);
    41. //设置固定长度
    42. this->setFixedSize(400,300);
    43. //设置窗口标题
    44. this->setWindowTitle("Widget");
    45. //设置窗口图标
    46. this->setWindowIcon(QIcon("C:\\Users\\wuhuiwu\\Desktop\\wodepeizhenshi.png"));
    47. //设置窗口尺寸
    48. btn1->setFixedSize(70,40);
    49. //移动按钮
    50. btn1->move(150,250);
    51. //再构造一个按钮,并给定父组件,图标,文本内容
    52. btn2=new QPushButton(QIcon("C:\\Users\\wuhuiwu\\Desktop\\cancel.png"),"取消",this);
    53. //设置按钮尺寸
    54. btn2->setFixedSize(70,40);
    55. //移动按钮
    56. btn2->move(btn1->x()+70,btn1->y());
    57. //再构造一个注册按钮,并给定父组件,图标,文本内容
    58. btn3=new QPushButton(QIcon("C:\\Users\\wuhuiwu\\Desktop\\register.png"),"注册",this);
    59. //设置按钮尺寸
    60. btn3->setFixedSize(70,40);
    61. //移动按钮
    62. btn3->move(btn2->x()+70,btn1->y());
    63. //实例化一个标签并指定父组件
    64. lab1=new QLabel(this);
    65. //设置尺寸
    66. lab1->resize(400,150);
    67. //设置图片
    68. lab1->setPixmap(QPixmap("C:\\Users\\wuhuiwu\\Desktop\\logo.png"));
    69. lab1->setScaledContents(true);
    70. //实例化一个标签并指定父组件
    71. lab2=new QLabel(this);
    72. //设置图片
    73. lab2->setPixmap(QPixmap("C:\\Users\\wuhuiwu\\Desktop\\userName.jpg"));
    74. //设置尺寸
    75. lab2->resize(40,30);
    76. //引动图标
    77. lab2->move(120,160);
    78. lab2->setScaledContents(true);
    79. //实例化一个标签并指定父组件
    80. lab3=new QLabel(this);
    81. //设置图片
    82. lab3->setPixmap(QPixmap("C:\\Users\\wuhuiwu\\Desktop\\passwd.jpg"));
    83. //设置尺寸
    84. lab3->resize(40,30);
    85. //移动图标
    86. lab3->move(lab2->x(),lab2->y()+50);
    87. lab3->setScaledContents(true);
    88. //构建一个文本编辑器
    89. edit1=new QLineEdit(this);
    90. //设置尺寸
    91. edit1->resize(100,30);
    92. edit1->move(lab2->x()+60,lab2->y());
    93. //设置占位文本
    94. edit1->setPlaceholderText("账号名");
    95. //构建一个文本编辑器
    96. edit2=new QLineEdit(this);
    97. //设置尺寸
    98. edit2->resize(100,30);
    99. edit2->move(edit1->x(),edit1->y()+50);
    100. //设置回显模式
    101. edit2->setEchoMode(QLineEdit::Password);
    102. //登录
    103. connect(this->btn1,&QPushButton::clicked,this,&Widget::userLogin);
    104. //退出
    105. connect(this->btn2,&QPushButton::clicked,this,&Widget::userExit);
    106. //注册
    107. connect(this->btn3,&QPushButton::clicked,this,&Widget::userregister);
    108. }
    109. Widget::~Widget()
    110. {
    111. delete ui;
    112. }
    113. void Widget::userLogin()
    114. {
    115. QString accout=this->edit1->text();
    116. QString password=this->edit2->text();
    117. if(accout=="admin"&&password=="123456")
    118. {
    119. qDebug()<<"匹配成功";
    120. QMessageBox box(QMessageBox::NoIcon,"success","登录成功",
    121. QMessageBox::Ok);
    122. int res=box.exec();
    123. if(res==QMessageBox::Ok)
    124. {
    125. this->close();
    126. emit mysignals();
    127. }
    128. }else
    129. {
    130. qDebug()<<"账户密码不匹配,是否重新登录";
    131. QMessageBox box1(QMessageBox::Critical,"error","账户密码错误",
    132. QMessageBox::Ok|QMessageBox::Cancel);
    133. int res=box1.exec();
    134. if(res==QMessageBox::Ok)
    135. {
    136. this->edit2->clear();
    137. }
    138. else if(res==QMessageBox::Cancel)
    139. {
    140. this->close();
    141. }
    142. }
    143. }
    144. void Widget::userExit()
    145. {
    146. QMessageBox box2(QMessageBox::Warning,"退出","要退出吗?",
    147. QMessageBox::Yes|QMessageBox::No);
    148. int res=box2.exec();
    149. if(res==QMessageBox::Yes)
    150. {
    151. this->close();
    152. }
    153. else if(res==QMessageBox::No)
    154. {
    155. this->edit1->clear();
    156. this->edit2->clear();
    157. }
    158. }
    159. void Widget::userregister()
    160. {
    161. //获取ui界面中要录入的数据
    162. QString userName = this->edit1->text();
    163. QString password=this->edit2->text();
    164. //要确保每个编辑器中都有数据
    165. if(userName.isEmpty() || password.isEmpty())
    166. {
    167. QMessageBox::information(this,"提示","请将信息填写完整");
    168. return;
    169. }
    170. QString sql = QString("insert into stu_info(userName,password) "
    171. "values('%1', '%2')").arg(userName).arg(password);
    172. QSqlQuery querry;
    173. if(!querry.exec(sql))
    174. {
    175. QMessageBox::information(this,"失败", "添加失败");
    176. return;
    177. }else
    178. {
    179. QMessageBox::information(this,"成功", "添加成功");
    180. }
    181. }

    second.cpp

    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. }
    9. void Second::newslot()
    10. {
    11. this->show(); //将自己界面进行展示
    12. }
    13. Second::~Second()
    14. {
    15. delete ui;
    16. }

    main.cpp

    1. #include "widget.h"
    2. #include "second.h"
    3. #include
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. Widget w;
    8. w.show();
    9. Second s;
    10. QObject::connect(&w,&Widget::mysignals,&s,&Second::newslot);
    11. return a.exec();
    12. }

    思维导图

  • 相关阅读:
    [附源码]Python计算机毕业设计Django校园生活服务平台
    git 分支代码合并到master主分支上或者master合并到开发分支
    [8]xss你清楚吗
    三面“有赞”Java岗斩获offer:Spring+JVM+并发锁+分布式+算法
    学习笔记-Windows 基础服务搭建
    python的深浅copy
    异构计算, GPU和框架选型指南
    DevEco Device Tool 3.0 Release 新版本发布,支持多人共享开发
    Docker Compose快速入门及实战
    Java刷题day24
  • 原文地址:https://blog.csdn.net/weixin_56612097/article/details/133149746