• DAY54


    chat.h

    1. #ifndef CHAT_H
    2. #define CHAT_H
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. namespace Ui {
    9. class chat;
    10. }
    11. class chat : public QWidget
    12. {
    13. Q_OBJECT
    14. public:
    15. explicit chat(QWidget *parent = nullptr);
    16. ~chat();
    17. void jump_slot();
    18. private slots:
    19. void on_pushbutton1_clicked();
    20. void on_pushbutton2_clicked();
    21. private:
    22. Ui::chat *ui;
    23. QSqlDatabase db;
    24. };
    25. #endif // CHAT_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. #include
    13. #include "chat.h"
    14. #include
    15. #include
    16. #include
    17. #include
    18. QT_BEGIN_NAMESPACE
    19. namespace Ui { class Widget; }
    20. QT_END_NAMESPACE
    21. class Widget : public QWidget
    22. {
    23. Q_OBJECT
    24. public:
    25. Widget(QWidget *parent = nullptr);
    26. ~Widget();
    27. private slots:
    28. void on_btn2_clicked();
    29. void on_btn3_clicked();
    30. void on_btn4_clicked();
    31. signals:
    32. void jump();
    33. private:
    34. Ui::Widget *ui;
    35. chat *s1;
    36. };
    37. #endif // WIDGET_H

    chat.cpp

    1. #include "chat.h"
    2. #include "ui_chat.h"
    3. chat::chat(QWidget *parent) :
    4. QWidget(parent),
    5. ui(new Ui::chat)
    6. {
    7. ui->setupUi(this);
    8. if(!db.contains("mydatabase.db"))
    9. {
    10. db = QSqlDatabase::addDatabase("QSQLITE");
    11. db.setDatabaseName("mydatabase.db");
    12. }
    13. if(!db.open())
    14. {
    15. QMessageBox::information(this," 失败","数据库打开失败");
    16. return ;
    17. }
    18. QString sql = "create table if not exists usr("
    19. "account varchar(10),"
    20. "password varchar(15))";
    21. QSqlQuery querry;
    22. if(!querry.exec(sql))
    23. {
    24. QMessageBox::information(this,"失败","创建表失败");
    25. return ;
    26. }
    27. }
    28. chat::~chat()
    29. {
    30. delete ui;
    31. }
    32. void chat::jump_slot()
    33. {
    34. this->show();
    35. }
    36. void chat::on_pushbutton1_clicked()
    37. {
    38. QString account = ui->pushbutton1->text();
    39. QString password = ui->pushbutton2->text();
    40. if(account.isEmpty() || password.isEmpty())
    41. {
    42. QMessageBox::information(this,"提示","请讲信息填写完整");
    43. return ;
    44. }
    45. QString sql = QString("insert into usr(account,password)"
    46. "values('%1','%2')").arg(account).arg(password);
    47. QSqlQuery querry;
    48. if(!querry.exec(sql))
    49. {
    50. QMessageBox::information(this,"失败","注册失败");
    51. return ;
    52. }
    53. else
    54. {
    55. QMessageBox::information(this,"成功","注册成功");
    56. }
    57. }
    58. void chat::on_pushbutton2_clicked()
    59. {
    60. this->close();
    61. }

    main.cpp

    1. #include "widget.h"
    2. #include
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6. Widget w;
    7. w.show();
    8. return a.exec();
    9. }

    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. s1 = new chat;
    9. connect(this,&Widget::jump,s1,&chat::jump_slot);
    10. this->setWindowTitle("221#223社畜登录框"); //设置窗口标题
    11. this->setWindowIcon(QIcon(":/new/prefix1/C:/Users/Administrator/Desktop/t/QQ.png")); //设置窗口图标
    12. this->setStyleSheet("background-color:skyblue;"); //设置窗口样式表
    13. this->setWindowOpacity(1); //设置窗口透明度
    14. ui->lab1->setPixmap(QPixmap(":/new/prefix1/C:/Users/Administrator/Desktop/t/(}OLKA@NAP5WIO@0SMS9OLG.png"));
    15. ui->lab1->setScaledContents(true); //填充标签
    16. ui->btn1->setIcon(QIcon(":/new/prefix1/C:/Users/Administrator/Desktop/t/QQ.png")); //设置图标
    17. ui->btn2->setIcon(QIcon(":/new/prefix1/C:/Users/Administrator/Desktop/t/QQ.png")); //设置图标
    18. ui->btn3->setIcon(QIcon(":/new/prefix1/C:/Users/Administrator/Desktop/t/QQ.png")); //设置图标
    19. ui->btn4->setStyleSheet("background-color:skyblue; border-radius:10px; ");
    20. }
    21. Widget::~Widget()
    22. {
    23. delete ui;
    24. }
    25. //登录按钮
    26. void Widget::on_btn2_clicked()
    27. {
    28. QString sql = "select * from usr";
    29. QSqlQuery querry;
    30. if(!querry.exec(sql))
    31. {
    32. QMessageBox::information(this,"提示","匹配失败");
    33. return ;
    34. }
    35. int i = 0;
    36. while(querry.next())
    37. {
    38. for(int j = 0; j < querry.record().count(); j++)
    39. {
    40. //登录失败
    41. if(ui->edit1->text() != querry.record().value(j) && ui->edit2->text() != querry.record().value(j))
    42. {
    43. //调用构造函数实例化对象
    44. QMessageBox box(QMessageBox::Critical,//图标
    45. "错误",//对话框标题
    46. "账号密码不匹配,是否重新登录",//对话框文本
    47. QMessageBox::Yes|QMessageBox::No,//提供的按钮
    48. this);//父组件
    49. box.setDefaultButton(QMessageBox::Yes);
    50. box.setButtonText(QMessageBox::Yes,"OK");
    51. box.setButtonText(QMessageBox::No,"cancel");
    52. //调用exec函数运行对话框
    53. int ret = box.exec();
    54. //对结果进行判断
    55. if(ret==QMessageBox::Yes)
    56. {
    57. //清空内容
    58. ui->edit1->clear();
    59. ui->edit2->clear();
    60. }
    61. else
    62. {
    63. //关闭界面
    64. this->close();
    65. }
    66. }
    67. //登录成功
    68. else
    69. {
    70. //调用构造函数实例化对象
    71. QMessageBox box(QMessageBox::NoIcon,//图标
    72. " ",//对话框标题
    73. "登录成功",//对话框文本
    74. QMessageBox::Ok,//提供的按钮
    75. this);//父组件
    76. box.exec();//运行对话框
    77. emit jump();//跳转界面
    78. this->close();//关闭当前界面
    79. }
    80. }
    81. i++;
    82. }
    83. }
    84. void Widget::on_btn3_clicked()
    85. {
    86. int ret=QMessageBox::warning(this,
    87. "警告",
    88. "是否确定要退出登录",
    89. QMessageBox::Yes | QMessageBox::No,
    90. QMessageBox::No);
    91. //对结果进行判断如果点yes则关闭
    92. if(ret==QMessageBox::Yes)
    93. {
    94. this->close();//关闭界面
    95. }
    96. }
    97. void Widget::on_btn4_clicked()
    98. {
    99. emit jump();
    100. }

  • 相关阅读:
    【MongoDB】docker部署社区版(一)
    基于身份的分段:三种技术路线解析与建议
    基于SSM的北京集联软件科技有限公司信息管理系统
    15 Python使用MySQL
    【C#设计模式】单例模式
    Java实现就医保险管理系统 JAVA+Vue+SpringBoot+MySQL
    CEC2015:(二)动态多目标野狗优化算法DMODOA求解DIMP2、dMOP2、dMOP2iso、dMOP2dec(提供Matlab代码)
    景联文科技提供高质量人像采集服务,助力3D虚拟人提升逼真度
    电源ATE自动测试系统为您提供一站式自动化测试解决方案
    ROBOGUIDE教程:三维模型创建功能介绍与操作方法
  • 原文地址:https://blog.csdn.net/weixin_52839062/article/details/133148787