• 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 //执行sql语句的类
    11. #include //数据库记录的类
    12. #include "form.h"
    13. #include
    14. QT_BEGIN_NAMESPACE
    15. namespace Ui { class MainWindow; }
    16. QT_END_NAMESPACE
    17. class MainWindow : public QMainWindow
    18. {
    19. Q_OBJECT
    20. public:
    21. MainWindow(QWidget *parent = nullptr);
    22. ~MainWindow();
    23. private:
    24. Ui::MainWindow *ui;
    25. QLineEdit *zh;
    26. QLineEdit *mm;
    27. Form *f1;
    28. QSqlDatabase db;
    29. signals:
    30. void my_sig();
    31. void jump();
    32. public slots:
    33. void btn1_slot();
    34. void btn2_slot();
    35. void btn3_slot();
    36. };
    37. #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. //设置固定大小
    9. this->setFixedSize(400,300);
    10. //设置窗口标题
    11. this->setWindowTitle("HUAQI");
    12. //设置窗口图标
    13. this->setWindowIcon(QIcon("D:\\Qt\\icon\\icon\\wodepeizhenshi.png"));
    14. //构建两个按钮
    15. QPushButton *btn1=new QPushButton(QIcon("D:\\Qt\\icon\\icon\\login.png"),"登录",this);
    16. btn1->resize(100,40);
    17. btn1->move(150,250);
    18. QPushButton *btn2=new QPushButton(QIcon("D:\\Qt\\icon\\icon\\cancel.png"),"取消",this);
    19. btn2->resize(btn1->size());
    20. btn2->move(btn1->x()+125,btn1->y());
    21. connect(btn2,SIGNAL(clicked()),this,SLOT(btn2_slot()));
    22. QPushButton *btn3=new QPushButton("注册",this);
    23. btn3->resize(btn1->size());
    24. btn3->move(btn1->x()-125,btn1->y());
    25. connect(btn3,SIGNAL(clicked()),this,SLOT(btn3_slot()));
    26. //构建两个行编辑器
    27. QLineEdit *edit1=new QLineEdit(this);
    28. edit1->resize(200,30);
    29. edit1->setEchoMode(QLineEdit::Password);
    30. edit1->setText("123456");
    31. edit1->move(125,btn1->y()-50);
    32. QLineEdit *edit2=new QLineEdit(this);
    33. edit2->resize(200,30);
    34. edit2->setText("admin");
    35. edit2->move(125,edit1->y()-50);
    36. //构建三个标签
    37. QLabel *lab1=new QLabel(this);
    38. lab1->resize(30,30);
    39. lab1->setPixmap(QPixmap("D:\\Qt\\icon\\icon\\passwd.jpg"));
    40. lab1->setScaledContents(true);
    41. lab1->move(edit1->x()-40,edit1->y());
    42. QLabel *lab2=new QLabel(this);
    43. lab2->resize(lab1->size());
    44. lab2->setPixmap(QPixmap("D:\\Qt\\icon\\icon\\userName.jpg"));
    45. lab2->setScaledContents(true);
    46. lab2->move(edit2->x()-40,edit2->y());
    47. QLabel *lab3=new QLabel(this);
    48. lab3->resize(400,120);
    49. lab3->setPixmap(QPixmap("D:\\Qt\\icon\\icon\\logo.png"));
    50. lab3->setScaledContents(true);
    51. connect(btn1,&QPushButton::clicked,this,&MainWindow::btn1_slot);
    52. zh=edit2;
    53. mm=edit1;
    54. //设置数据库
    55. if(!db.contains("mydb.db"))
    56. {
    57. db=QSqlDatabase::addDatabase("QSQLITE");
    58. db.setDatabaseName("mydb.db");
    59. }
    60. //打开数据库
    61. if(!db.open())
    62. {
    63. QMessageBox::information(this,"失败","数据库打开失败");
    64. return;
    65. }
    66. //创建表
    67. QString sql="create table if not exists stu_info("
    68. "zhanghao varchar(20) primary key,"
    69. "mima varchar(20))";
    70. QSqlQuery querry;
    71. if(!querry.exec(sql))
    72. {
    73. QMessageBox::information(this,"失败","创建表失败");
    74. return;
    75. }
    76. }
    77. MainWindow::~MainWindow()
    78. {
    79. delete ui;
    80. }
    81. void MainWindow::btn1_slot()
    82. {
    83. QString i=zh->text();
    84. QString j=mm->text();
    85. qDebug()<<i;
    86. qDebug()<<j;
    87. QString sql=QString("select * from stu_info");
    88. QSqlQuery querry;
    89. if(!querry.exec(sql))
    90. {
    91. QMessageBox::information(this,"失败","查询失败");
    92. return;
    93. }
    94. while(querry.next())
    95. {
    96. qDebug()<<querry.record().value(0).toString();
    97. qDebug()<<querry.record().value(1).toString();
    98. if(querry.record().value(0).toString()==i&&querry.record().value(1).toString()==j)
    99. {
    100. QMessageBox box(QMessageBox::NoIcon,
    101. "成功",
    102. "登陆成功",
    103. QMessageBox::Ok,
    104. this);
    105. int ret=box.exec();
    106. if(ret==QMessageBox::Ok)
    107. {
    108. emit jump();
    109. }
    110. return;
    111. }
    112. }
    113. QMessageBox box(QMessageBox::Warning,
    114. "错误",
    115. "账号或密码出错",
    116. QMessageBox::Yes|QMessageBox::No,
    117. this);
    118. box.setButtonText(QMessageBox::Yes,"继续");
    119. box.setButtonText(QMessageBox::No,"取消");
    120. int ret=box.exec();
    121. if(ret==QMessageBox::Yes)
    122. {
    123. }else
    124. {
    125. this->close();
    126. }
    127. return;
    128. }
    129. void MainWindow::btn2_slot()
    130. {
    131. QMessageBox box(QMessageBox::Question,
    132. "退出",
    133. "确定要退出吗?",
    134. QMessageBox::Yes|QMessageBox::No,
    135. this);
    136. int ret=box.exec();
    137. if(ret==QMessageBox::Yes)
    138. {
    139. this->close();
    140. }
    141. }
    142. void MainWindow::btn3_slot()
    143. {
    144. QString zhanghao=zh->text();
    145. QString mima=mm->text();
    146. if(zhanghao.isEmpty()||mima.isEmpty())
    147. {
    148. QMessageBox::information(this,"提示","信息不完整");
    149. return;
    150. }
    151. QString sql=QString("insert into stu_info(zhanghao,mima)"
    152. "values('%1','%2')").arg(zhanghao).arg(mima);
    153. QSqlQuery querry;
    154. if(!querry.exec(sql))
    155. {
    156. QMessageBox::information(this,"失败","添加失败");
    157. return;
    158. }else
    159. {
    160. QMessageBox::information(this,"成功","添加成功");
    161. }
    162. }

    效果图

    添加

    登录

  • 相关阅读:
    Linux Troubleshooting 超实用系列 - Disk Analysis
    Ps:裁剪工具 - 裁剪预设的应用
    如何发挥影响力,达成目标(一对一场景)
    codeforces-1734C - Removing Smallest Multiples
    DateTime6
    【项目实践-04】实验室移动端:对象添加新属性+webview IOS端appendJsFile()方法不生效
    C++原子操作和互斥锁性能(速度)对比
    智慧地铁| 云计算为城市地铁交通注入智慧
    记一次在amd架构打包arm64架构的镜像的试错经历
    Redis入门:Redis持久化策略RDB&AOF简介
  • 原文地址:https://blog.csdn.net/m0_70569664/article/details/133148622