• QT&C++ day12


    注册登录界面

    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 "second.h"
    11. #include //数据库管理类
    12. #include //执行sql语句类
    13. #include //数据库记录的类
    14. #include "zhuce.h" //注册头文件
    15. QT_BEGIN_NAMESPACE
    16. namespace Ui { class widget; }
    17. QT_END_NAMESPACE
    18. class widget : public QWidget
    19. {
    20. Q_OBJECT
    21. signals://该权限下定义属于自己的信号
    22. void my_signal(QString msg);//自定义一个有参无返回值的信号函数
    23. private:
    24. void on_btn1_clicked();//自定义的槽函数声明
    25. void on_btn2_clicked();//自定义的槽函数声明
    26. void on_btn3_clicked();
    27. public:
    28. widget(QWidget *parent = nullptr);
    29. ~widget();
    30. signals:
    31. void jump();//自定义跳转信号函数
    32. void jump1();
    33. public:
    34. //void jump_slot2();
    35. private:
    36. Ui::widget *ui;
    37. //自定义一个btn1
    38. QPushButton *btn1;
    39. QPushButton *btn2;
    40. QPushButton *btn3;
    41. QLineEdit *edit2;
    42. QLineEdit *edit1;
    43. Second *s1;
    44. QSqlDatabase db; //定义一个数据库的类对象
    45. zhuce *s2;
    46. };
    47. #endif // WIDGET_H

    zhuce.h

    1. #ifndef ZHUCE_H
    2. #define ZHUCE_H
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include //执行sql语句类
    11. #include //数据库记录的类
    12. //#include "widget.h"
    13. namespace Ui {
    14. class zhuce;
    15. }
    16. class zhuce : public QWidget
    17. {
    18. Q_OBJECT
    19. private:
    20. void on_btn2_clicked();//自定义的槽函数声明
    21. void on_btn3_clicked();
    22. public:
    23. void jump_slot1();
    24. signals:
    25. //void jump2();
    26. public:
    27. explicit zhuce(QWidget *parent = nullptr);
    28. ~zhuce();
    29. private:
    30. Ui::zhuce *ui;
    31. QLineEdit *edit2;//密码
    32. QLineEdit *edit1;//用户名
    33. QLineEdit *edit3;//确认密码
    34. QPushButton *btn2;
    35. QPushButton *btn3;
    36. QSqlDatabase db; //定义一个数据库的类对象
    37. //widget *s3;
    38. };
    39. #endif // ZHUCE_H

    widget.cpp

    1. #include "widget.h"
    2. widget::widget(QWidget *parent)
    3. : QWidget(parent)
    4. {
    5. //登录成功的界面
    6. s1=new Second;
    7. connect(this,&widget::jump,s1,&Second::jump_slot);
    8. //注册的界面
    9. s2=new zhuce;
    10. connect(this,&widget::jump1,s2,&zhuce::jump_slot1);
    11. //添加数据库
    12. if(!db.contains("mysql.db"))
    13. {
    14. //添加数据库
    15. db=QSqlDatabase::addDatabase("QSQLITE");
    16. //设置数据库名字
    17. db.setDatabaseName("mysql.db");
    18. }
    19. //打开数据库
    20. if(!db.open())
    21. {
    22. QMessageBox::information(this,"失败","数据库打开失败");
    23. return;
    24. }
    25. //准备sql语句对表进行创建
    26. QString sql="create table if not exists stu_info("
    27. "name varchar(10) primary key,"//用户名
    28. "password varchar(20))";//密码
    29. //语句执行者
    30. QSqlQuery querry;
    31. if(!querry.exec(sql))
    32. {
    33. QMessageBox::information(this,"失败","数据库创建失败");
    34. return;
    35. }
    36. this->setFixedSize(550,400);//设置固定尺寸
    37. this->setWindowTitle("Widget");//设置窗口标题
    38. this->setWindowIcon(QIcon("D:\\icon\\wodepeizhenshi.png"));//设置窗口图标
    39. //实例化一个标签
    40. QLabel *lab1 = new QLabel;
    41. lab1->setParent(this);
    42. lab1->resize(550,215);//重新设置尺寸
    43. //lab1->setAlignment(Qt::AlignCenter);//文本对齐
    44. lab1->setPixmap(QPixmap("D:\\icon\\logo.png"));
    45. lab1->setScaledContents(true);//设置内容自适应
    46. QLabel *lab3 = new QLabel;
    47. lab3->setParent(this);//指定父组件
    48. lab3->resize(40,40);//重新设置尺寸
    49. lab3->move(100,220);
    50. //lab3->setAlignment(Qt::AlignCenter);
    51. lab3->setPixmap(QPixmap("D:\\icon\\userName.jpg"));
    52. lab3->setScaledContents(true);//设置内容自适应
    53. QLabel *lab2 = new QLabel;
    54. lab2->setParent(this);
    55. lab2->resize(40,40);//重新设置尺寸
    56. lab2->move(100,270);
    57. lab2->setPixmap(QPixmap("D:\\icon\\passwd.jpg"));
    58. lab2->setScaledContents(true);//设置内容自适应
    59. // 实例化一个行编辑器
    60. edit1 = new QLineEdit(this);
    61. edit1->resize(250,40);
    62. edit1->move(lab3->x()+50,lab3->y());
    63. // 实例化一个行编辑器
    64. edit2 = new QLineEdit(this);
    65. edit2->resize(250,40);
    66. edit2->move(lab2->x()+50,lab2->y());
    67. edit2->setEchoMode(QLineEdit::Password);//设置回显模式
    68. //实例化一个按钮并给定图标,文本内容,父组件
    69. btn1 = new QPushButton(QIcon("D:\\icon\\login.png"), "登录", this);
    70. btn1->resize(100,50);//设置按钮大小
    71. btn1->move(225,325);//设置按钮移动位置
    72. connect(this->btn1,&QPushButton::clicked,this,&widget::on_btn1_clicked);
    73. btn3 = new QPushButton(QIcon("D:\\icon\\R-C.jpg"), "注册", this);
    74. btn3->resize(btn1->size());//设置按钮大小
    75. btn3->move(btn1->x()-150,btn1->y());//设置按钮移动位置
    76. connect(this->btn3,&QPushButton::clicked,this,&widget::on_btn3_clicked);
    77. btn2 = new QPushButton(QIcon("D:\\icon\\cancel.png"),"取消", this);
    78. btn2->resize(btn1->size());
    79. btn2->move(btn1->x()+150,btn1->y());
    80. connect(this->btn2,&QPushButton::clicked,this,&widget::on_btn2_clicked);
    81. }
    82. //登录按钮
    83. void widget::on_btn1_clicked()
    84. {
    85. //获取界面姓名
    86. QString name = this->edit1->text();
    87. //准备sql语句
    88. QString sql = QString("select * from stu_info where name='%1'").arg(name);
    89. //准备执行者
    90. QSqlQuery querry;
    91. if(!querry.exec(sql))
    92. {
    93. QMessageBox::information(this, "提示", "登录失败");
    94. return;
    95. }
    96. else
    97. {
    98. //1、调用构造函数实例化对象
    99. QMessageBox box(QMessageBox::Information,//图标
    100. "信息对话框",//对话框标题
    101. "登录成功",//对话框文本内容
    102. QMessageBox::Ok,//提供的按钮
    103. this);//父组件
    104. box.setDefaultButton(QMessageBox::Ok);//将OK设置为默认按钮
    105. //2、调用exec函数运行对话框
    106. int ret=box.exec();
    107. //3、对结果进行判断
    108. if(ret==QMessageBox::Ok)
    109. {
    110. emit jump();
    111. this->hide();
    112. }
    113. }
    114. }
    115. //取消按钮
    116. void widget::on_btn2_clicked()
    117. {
    118. //1、调用构造函数实例化对象
    119. QMessageBox box(QMessageBox::Question,//图标
    120. "问题对话框",//对话框标题
    121. "是否确定取消登录?",//对话框文本内容
    122. QMessageBox::Yes|QMessageBox::No,//提供的按钮
    123. this);//父组件
    124. box.setDefaultButton(QMessageBox::No);//将no设置为默认按钮
    125. //2、调用exec函数运行对话框
    126. int ret=box.exec();
    127. //3、对结果进行判断
    128. if(ret==QMessageBox::Yes)
    129. {
    130. close();
    131. }
    132. else if(ret==QMessageBox::No)
    133. {
    134. }
    135. }
    136. void widget::on_btn3_clicked()
    137. {
    138. emit jump1();
    139. //QWidget::hide();
    140. }
    141. widget::~widget()
    142. {
    143. }
    144. /*void widget::jump_slot2()
    145. {
    146. this->show();
    147. }*/

    zhuce.cpp

    1. #include "zhuce.h"
    2. #include "ui_zhuce.h"
    3. zhuce::zhuce(QWidget *parent) :
    4. QWidget(parent),
    5. ui(new Ui::zhuce)
    6. {
    7. //注册的界面
    8. //s3=new widget;
    9. //connect(this,&zhuce::jump2,s3,&widget::jump_slot2);
    10. this->setFixedSize(550,500);//设置固定尺寸
    11. this->setWindowTitle("注册");//设置窗口标题
    12. this->setWindowIcon(QIcon("D:\\icon\\wodepeizhenshi.png"));//设置窗口图标
    13. //实例化一个标签
    14. QLabel *lab = new QLabel;
    15. lab->setParent(this);
    16. lab->resize(550,215);//重新设置尺寸
    17. //lab1->setAlignment(Qt::AlignCenter);//文本对齐
    18. lab->setPixmap(QPixmap("D:\\icon\\logo.png"));
    19. lab->setScaledContents(true);//设置内容自适应
    20. ui->setupUi(this);
    21. QLabel *lab1 = new QLabel;
    22. lab1->setParent(this);//指定父组件
    23. lab1->resize(150,40);//重新设置尺寸
    24. lab1->move(100,220);
    25. //lab3->setAlignment(Qt::AlignCenter);
    26. lab1->setText("用户名:");
    27. lab1->setScaledContents(true);//设置内容自适应
    28. QLabel *lab2 = new QLabel;
    29. lab2->setParent(this);
    30. lab2->resize(150,40);//重新设置尺寸
    31. lab2->move(100,270);
    32. lab2->setText("密码:");
    33. lab2->setScaledContents(true);//设置内容自适应
    34. QLabel *lab3 = new QLabel;
    35. lab3->setParent(this);
    36. lab3->resize(150,40);//重新设置尺寸
    37. lab3->move(100,320);
    38. lab3->setText("确认密码:");
    39. lab3->setScaledContents(true);//设置内容自适应
    40. // 实例化一个行编辑器
    41. edit1 = new QLineEdit(this);
    42. edit1->resize(250,40);
    43. edit1->move(lab1->x()+80,lab1->y());
    44. // 实例化一个行编辑器
    45. edit2 = new QLineEdit(this);
    46. edit2->resize(250,40);
    47. edit2->move(lab2->x()+80,lab2->y());
    48. edit2->setEchoMode(QLineEdit::Password);//设置回显模式
    49. edit3 = new QLineEdit(this);
    50. edit3->resize(250,40);
    51. edit3->move(lab3->x()+80,lab3->y());
    52. edit3->setEchoMode(QLineEdit::Password);//设置回显模式
    53. btn3 = new QPushButton(QIcon("D:\\icon\\R-C.jpg"), "注册", this);
    54. btn3->resize(100,50);//设置按钮大小
    55. btn3->move(125,400);//设置按钮移动位置
    56. connect(this->btn3,&QPushButton::clicked,this,&zhuce::on_btn3_clicked);
    57. btn2 = new QPushButton(QIcon("D:\\icon\\cancel.png"),"取消", this);
    58. btn2->resize(100,50);
    59. btn2->move(325,400);
    60. connect(this->btn2,&QPushButton::clicked,this,&zhuce::on_btn2_clicked);
    61. //添加数据库
    62. if(!db.contains("mysql.db"))
    63. {
    64. //添加数据库
    65. db=QSqlDatabase::addDatabase("QSQLITE");
    66. //设置数据库名字
    67. db.setDatabaseName("mysql.db");
    68. }
    69. //打开数据库
    70. if(!db.open())
    71. {
    72. QMessageBox::information(this,"失败","数据库打开失败");
    73. return;
    74. }
    75. }
    76. zhuce::~zhuce()
    77. {
    78. delete ui;
    79. }
    80. //取消
    81. void zhuce::on_btn2_clicked()
    82. {
    83. //1、调用构造函数实例化对象
    84. QMessageBox box(QMessageBox::Question,//图标
    85. "问题对话框",//对话框标题
    86. "是否确定取消注册?",//对话框文本内容
    87. QMessageBox::Yes|QMessageBox::No,//提供的按钮
    88. this);//父组件
    89. box.setDefaultButton(QMessageBox::No);//将no设置为默认按钮
    90. //2、调用exec函数运行对话框
    91. int ret=box.exec();
    92. //3、对结果进行判断
    93. if(ret==QMessageBox::Yes)
    94. {
    95. close();
    96. }
    97. else if(ret==QMessageBox::No)
    98. {
    99. }
    100. }
    101. void zhuce::on_btn3_clicked()
    102. {
    103. //记录要录入的数据
    104. QString name=this->edit1->text();
    105. QString password=this->edit3->text();
    106. if(this->edit2->text()!=this->edit3->text()&&this->edit2->text()!=0&&this->edit3->text()!=0)
    107. {
    108. QMessageBox::information(this,"失败","请输入相同的密码");
    109. return;
    110. }
    111. //准备sql语句
    112. QString sql=QString("insert into stu_info(name,password)"
    113. "values('%1','%2')").arg(name).arg(password);
    114. //准备执行语句者
    115. QSqlQuery querry;
    116. if(!querry.exec(sql))
    117. {
    118. QMessageBox::information(this,"失败","注册失败");
    119. return;
    120. }
    121. else
    122. {
    123. QMessageBox::information(this,"成功","注册成功");
    124. //emit jump2();
    125. this->edit1->clear();
    126. this->edit2->clear();
    127. this->edit3->clear();
    128. this->close();
    129. //parentWidget()->show();
    130. }
    131. }
    132. void zhuce::jump_slot1()
    133. {
    134. this->show();
    135. }

    思维导图

  • 相关阅读:
    Web渗透:文件包含漏洞(part.1)
    【微机原理|课程报告】双机并口通信课设报告
    微软如何打造数字零售力航母系列科普01 --- Azure顾问(AZURE Advisor)简介
    360+城市空气质量指数-日度数据、良好天数统计(2001-2022年)
    1、强化学习基础知识点
    Elasticsearch 8.3中新增和改进的亮点!
    [NCTF2019]Fake XML cookbook XML注入
    centos下的yum安装出现的问题
    Java的<? super T>和<? extends R>理解与应用
    【MD5】采用MD5+盐的加密方式完成注册用户和登录账号
  • 原文地址:https://blog.csdn.net/m0_59031281/article/details/133148740