• QT day5


    数据库完成登入注册

    mainwindow.h

    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"form.h"
    12. /*******数据库***********/
    13. #include//数据库
    14. #include
    15. #include
    16. #include
    17. QT_BEGIN_NAMESPACE
    18. namespace Ui { class MainWindow; }
    19. QT_END_NAMESPACE
    20. class MainWindow : public QMainWindow
    21. {
    22. Q_OBJECT
    23. signals:
    24. void jump();//自定义跳转信号函数
    25. void jump2();//自定义跳转信号函数
    26. private slots:
    27. void on_btn1_clicked();
    28. void on_btn2_clicked();
    29. void on_btn3_clicked();
    30. public:
    31. MainWindow(QWidget *parent = nullptr);
    32. ~MainWindow();
    33. private:
    34. Ui::MainWindow *ui;
    35. QPushButton *btn1;
    36. QPushButton *btn2;
    37. QPushButton *btn3;
    38. QLineEdit *edit1;
    39. QLineEdit *edit2;
    40. QSqlDatabase mydb;//数据库
    41. };
    42. #endif // MAINWINDOW_H

    mainwindow.cpp

    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. this->setFixedSize(600,400);//设置固定尺寸
    9. this->setWindowIcon(QIcon(":/new/prefix1/tuku/yh.png"));//改变左上角图标的位
    10. this->setWindowTitle("易碗浆糊");//窗口的名字
    11. //this->setStyleSheet("background-color:pink;");//背景颜色
    12. QLabel *lab1=new QLabel(this);//实例化一个标签
    13. lab1->resize(600,170);//重新设置尺寸
    14. lab1->setStyleSheet("background-color:yellow");
    15. lab1->setAlignment(Qt::AlignCenter);
    16. lab1->setPixmap(QPixmap(":/new/prefix1/tuku/fjh.png"));
    17. lab1->setScaledContents(true);
    18. this->setStyleSheet("background-color:white;");//背景颜色
    19. //用户名框
    20. edit1=new QLineEdit(this);
    21. edit1->resize(230,45);
    22. edit1->move(lab1->x()+200,lab1->y()+200);
    23. edit1->setPlaceholderText("用户名");//设置占位文本
    24. //密码框
    25. edit2=new QLineEdit(this);
    26. edit2->resize(230,45);
    27. edit2->move(edit1->x(),edit1->y()+75);
    28. edit2->setPlaceholderText("密码");//设置占位文本
    29. edit2->setEchoMode(QLineEdit::Password);//设置回显模式
    30. //实例化一个标签
    31. QLabel *lab2=new QLabel(this);
    32. lab2->resize(45,45);//重新设置尺寸
    33. lab2->setAlignment(Qt::AlignCenter);
    34. lab2->setPixmap(QPixmap(":/new/prefix1/tuku/wxn.png"));
    35. lab2->setScaledContents(true);
    36. lab2->move(lab1->x()+140,lab1->y()+200);
    37. //实例化一个标签
    38. QLabel *lab3=new QLabel(this);
    39. lab3->resize(45,45);//重新设置尺寸
    40. lab3->setAlignment(Qt::AlignCenter);
    41. lab3->setPixmap(QPixmap(":/new/prefix1/tuku/wxnnn.png"));
    42. lab3->setScaledContents(true);
    43. lab3->move(lab1->x()+140,lab1->y()+275);
    44. //实例化一个按钮
    45. //QPushButton *btn1=new QPushButton(this);
    46. this->btn1=new QPushButton("btn1",this);
    47. btn1->setText("登录");
    48. btn1->resize(70,40);
    49. btn1->move(lab3->x()+80,lab3->y()+70);
    50. btn1->setIcon(QIcon(":/new/prefix1/tuku/wq.png"));
    51. connect(btn1,&QPushButton::clicked,this,&MainWindow::on_btn1_clicked);
    52. // btn1->setStyleSheet("background-color:white;border-radius:10px;");//设置组件背景色
    53. //实例化一个按钮
    54. // QPushButton *btn2=new QPushButton(this);
    55. this->btn2=new QPushButton("btn2",this);
    56. btn2->setText("取消");
    57. btn2->resize(70,40);
    58. btn2->move(btn1->x()+130,btn1->y());
    59. // btn2->setStyleSheet("background-color:white;border-radius:10px;");//设置组件背景色
    60. btn2->setIcon(QIcon(":/new/prefix1/tuku/wq2.png"));
    61. connect(btn2,&QPushButton::clicked,this,&MainWindow::on_btn2_clicked);
    62. //实例化一个按钮
    63. this->btn3=new QPushButton("btn2",this);
    64. btn3->setText("注册");
    65. btn3->resize(70,45);
    66. btn3->move(edit1->x()+235,edit1->y());
    67. connect(btn3,&QPushButton::clicked,this,&MainWindow::on_btn3_clicked);
    68. /************************************************/
    69. if(!mydb.contains("mydatabase.db"))
    70. {
    71. mydb=QSqlDatabase::addDatabase("QSQLITE");
    72. //设置数据库的名字
    73. mydb.setDatabaseName("mydatabase.db");
    74. }
    75. if(!mydb.open())
    76. {
    77. QMessageBox::information(this,"失败","数据库打开失败");
    78. return;
    79. }
    80. //准备sql语句
    81. QString sql="create table if not exists user_info("
    82. "name varchar(10),"//用户名
    83. "mm integer)";//密码
    84. }
    85. void MainWindow::on_btn1_clicked()
    86. {
    87. //准备sql语句
    88. QString sql="select *from user_info";
    89. //准备语句执行者
    90. QSqlQuery querry;
    91. //执行sql语句
    92. if(!querry.exec(sql))
    93. {
    94. QMessageBox::information(this,"提示","执行失败");
    95. return;
    96. }
    97. int flg=0;
    98. QSqlRecord rec = querry.record();//总行
    99. while(querry.next())
    100. {
    101. for(int i=0;icount();i++)
    102. { if(edit1->text()==querry.record().value(0).toString() &&edit2->text()==querry.record().value(1).toString()){
    103. flg=1;
    104. break;
    105. }
    106. }
    107. }
    108. if(flg==1)//登入成功
    109. {
    110. emit jump();
    111. this->hide();
    112. }
    113. else if(flg==0)//数据库没有相应的密码和用户
    114. {
    115. QMessageBox box(QMessageBox::Critical,"密码错误","账号密码不匹配,是否重新登录",
    116. QMessageBox::Yes|QMessageBox::No,
    117. this);
    118. box.setButtonText(QMessageBox::Yes,"OK");
    119. box.setButtonText(QMessageBox::No,"cancel");
    120. int ret=box.exec();
    121. if(ret==QMessageBox::Yes)
    122. {
    123. edit1->clear();//清空
    124. edit2->clear();//清空
    125. }else if(ret==QMessageBox::No)
    126. {
    127. this->close();
    128. }
    129. }
    130. if(edit1->text()=="admin" &&edit2->text()=="12345")
    131. {
    132. emit jump();
    133. this->hide();
    134. }
    135. }
    136. void MainWindow::on_btn2_clicked()
    137. {
    138. int ret1= QMessageBox::question(this,"问题","是否确定要退出登录",QMessageBox::Yes|QMessageBox::No,QMessageBox::No);
    139. //对用户选中的按钮进行判断
    140. if(ret1==QMessageBox::Yes)
    141. {
    142. this->close();
    143. }else if(ret1==QMessageBox::No)
    144. {
    145. edit1->clear();//清空
    146. edit2->clear();//清空
    147. }
    148. }
    149. //注册按钮的槽函数
    150. void MainWindow::on_btn3_clicked()
    151. {
    152. emit jump2();
    153. this->hide();
    154. }
    155. MainWindow::~MainWindow()
    156. {
    157. delete ui;
    158. }

    from.h

    1. #ifndef FORM_H
    2. #define FORM_H
    3. #include//消息对话框
    4. #include
    5. #include
    6. namespace Ui {
    7. class Form;
    8. }
    9. class Form : public QWidget
    10. {
    11. Q_OBJECT
    12. public slots:
    13. void jump_slot();
    14. void on_bt3_clicked();
    15. public:
    16. explicit Form(QWidget *parent = nullptr);
    17. ~Form();
    18. private:
    19. Ui::Form *ui;
    20. };
    21. #endif // FORM_H

    form.cpp

    1. #include "form.h"
    2. #include "ui_form.h"
    3. Form::Form(QWidget *parent) :
    4. QWidget(parent),
    5. ui(new Ui::Form)
    6. {
    7. ui->setupUi(this);
    8. this->setWindowIcon(QIcon(":/new/prefix1/tuku/yh.png"));//改变左上角图标的位
    9. this->setWindowTitle("成功");//窗口的名字
    10. }
    11. Form::~Form()
    12. {
    13. delete ui;
    14. }
    15. //跳转
    16. void Form::jump_slot()
    17. {
    18. this->show();
    19. }
    20. void Form::on_bt3_clicked()
    21. {
    22. close();
    23. }

    zcfrom.h

    1. #ifndef ZCFROM_H
    2. #define ZCFROM_H
    3. #include//消息对话框
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. #include//消息对话框 //输出函数对应的头文件
    13. #include
    14. #include"form.h"
    15. /*******数据库***********/
    16. #include//数据库
    17. #include
    18. #include
    19. #include
    20. namespace Ui {
    21. class zcfrom;
    22. }
    23. class zcfrom : public QWidget
    24. {
    25. Q_OBJECT
    26. signals:
    27. void jump3();//自定义跳转信号函数
    28. public slots:
    29. void jump2_slot();
    30. public:
    31. explicit zcfrom(QWidget *parent = nullptr);
    32. ~zcfrom();
    33. private slots:
    34. void on_zcBttn_clicked();
    35. void on_fhbttn_clicked();
    36. private:
    37. Ui::zcfrom *ui;
    38. QSqlDatabase mydb;//数据库
    39. };
    40. #endif // ZCFROM_H

    zcfrom.cpp

    1. #include "zcfrom.h"
    2. #include "ui_zcfrom.h"
    3. zcfrom::zcfrom(QWidget *parent) :
    4. QWidget(parent),
    5. ui(new Ui::zcfrom)
    6. {
    7. ui->setupUi(this);
    8. this->setWindowTitle("注册");//窗口的名字
    9. this->setWindowIcon(QIcon(":/new/prefix1/tuku/yh.png"));//改变左上角图标的位
    10. if(!mydb.contains("mydatabase.db"))
    11. {
    12. mydb=QSqlDatabase::addDatabase("QSQLITE");
    13. //设置数据库的名字
    14. mydb.setDatabaseName("mydatabase.db");
    15. }
    16. if(!mydb.open())
    17. {
    18. QMessageBox::information(this,"失败","数据库打开失败");
    19. return;
    20. }
    21. //准备sql语句
    22. QString sql="create table if not exists user_info("
    23. "name varchar(10),"//用户名
    24. "mm integer)";//密码
    25. //准备语句执行者
    26. QSqlQuery querry;
    27. //让语句执行者指向sql语句
    28. //成功true失败false
    29. if(!querry.exec(sql))
    30. {
    31. QMessageBox::information(this,"失败","失败");
    32. return;
    33. }
    34. }
    35. zcfrom::~zcfrom()
    36. {
    37. delete ui;
    38. }
    39. void zcfrom::jump2_slot()
    40. {
    41. this->show();
    42. }
    43. //确定
    44. void zcfrom::on_zcBttn_clicked()
    45. {
    46. QString name=ui->nameEdit->text();
    47. int mm=ui->mmEdit->text().toInt();
    48. if(name.isEmpty()||mm==0)
    49. {
    50. QMessageBox::information(this,"提示","数据未填写完整");
    51. return;
    52. }
    53. //准备sql语句
    54. QString sql=QString("insert into user_info(name,mm)"
    55. "values('%1',%4)").arg(name).arg(mm);
    56. QSqlQuery querry;
    57. if(!querry.exec(sql))
    58. {
    59. QMessageBox::information(this,"失败","注册失败");
    60. return;
    61. }else
    62. {
    63. ui->nameEdit->clear();
    64. ui->mmEdit->clear();
    65. QMessageBox::information(this,"成功","注册成功");
    66. //此界面消失
    67. emit jump3();
    68. this->hide();
    69. }
    70. }
    71. //返回登录按钮的槽函数
    72. void zcfrom::on_fhbttn_clicked()
    73. {
    74. emit jump3();
    75. this->hide();
    76. }

    main.cpp

    1. #include "mainwindow.h"
    2. #include "form.h"
    3. #include
    4. #include "zcfrom.h"
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8. MainWindow w;
    9. w.show();
    10. Form s;//定义第二个界面
    11. QObject::connect(&w,&MainWindow::jump,&s,&Form::jump_slot);
    12. zcfrom s2;//定义第二个界面
    13. QObject::connect(&w,&MainWindow::jump2,&s2,&zcfrom::jump2_slot);
    14. QObject::connect(&s2,&zcfrom::jump3,&w,&MainWindow::show);//定义第二个界面
    15. return a.exec();
    16. }

    03dljm.pro

    1. QT += core gui sql
    2. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    3. CONFIG += c++11
    4. # The following define makes your compiler emit warnings if you use
    5. # any Qt feature that has been marked deprecated (the exact warnings
    6. # depend on your compiler). Please consult the documentation of the
    7. # deprecated API in order to know how to port your code away from it.
    8. DEFINES += QT_DEPRECATED_WARNINGS
    9. # You can also make your code fail to compile if it uses deprecated APIs.
    10. # In order to do so, uncomment the following line.
    11. # You can also select to disable deprecated APIs only up to a certain version of Qt.
    12. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
    13. SOURCES += \
    14. form.cpp \
    15. main.cpp \
    16. mainwindow.cpp \
    17. zcfrom.cpp
    18. HEADERS += \
    19. form.h \
    20. mainwindow.h \
    21. zcfrom.h
    22. FORMS += \
    23. form.ui \
    24. mainwindow.ui \
    25. zcfrom.ui
    26. # Default rules for deployment.
    27. qnx: target.path = /tmp/$${TARGET}/bin
    28. else: unix:!android: target.path = /opt/$${TARGET}/bin
    29. !isEmpty(target.path): INSTALLS += target
    30. RESOURCES += \
    31. ttp.qrc
    32. INCLUDEPATH += D:/opencv/opencv3.4-qt-intall/install/include
    33. INCLUDEPATH += D:/opencv/opencv3.4-qt-intall/install/include/opencv
    34. INCLUDEPATH += D:/opencv/opencv3.4-qt-intall/install/include/opencv2
    35. LIBS += D:/opencv/opencv3.4-qt-intall/install/x86/mingw/lib/libopencv_*.a

     

    智能指针思维导图

  • 相关阅读:
    JMeter界面和字体的调整
    减少Spring Boot的JVM内存占用的Docker三种配置
    算法之冒泡排序
    如何利用PCB创建PCB封装库
    数字化时代,低代码是企业的未来共识吗?
    Python3 Selenium4 chromedriver Pycharm闪退的问题
    Redis的安装教程(Windows+Linux)【超详细】
    【送书活动】全网超50万粉丝的Linux大咖良许,出书了!
    2022/6/30学习总结
    Android multiple back stacks导航的几种实现
  • 原文地址:https://blog.csdn.net/HYL1234511/article/details/133148007