• QT---day2---9.18


    完善登录框

    点击登录按钮后,判断账号(admin)和密码(123456)是否一致,如果匹配失败,则弹出错误对话框,文本内容“账号密码不匹配,是否重新登录”,给定两个按钮ok和cancel,点击ok后,会清除密码框中的内容,继续进行登录;如果点击cancel按钮,则关闭界面。

    如果账号和密码匹配,则弹出信息对话框,给出提示信息为“登录成功”,给出一个按钮ok,点击ok后,关闭整个登录界面,跳转到其他界面

    点击取消按钮后,弹出问题对话框,询问是否确定要退出登录,给出两个按钮,yes|no,点击yes,则直接关闭整个登录界面,如果点击no则进行进行登录

    要求:消息对话框,对象版和静态成员函数版至少各实现一个

    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. QT_BEGIN_NAMESPACE
    10. namespace Ui { class Widget; }
    11. QT_END_NAMESPACE
    12. class Widget : public QWidget
    13. {
    14. Q_OBJECT
    15. public:
    16. Widget(QWidget *parent = nullptr);
    17. ~Widget();
    18. signals:
    19. void mysignals();
    20. public slots:
    21. void userLogin();
    22. void userExit();
    23. private:
    24. QPushButton *btn1;
    25. QPushButton *btn2;
    26. QLabel *lab1;
    27. QLabel *lab2;
    28. QLabel *lab3;
    29. QLineEdit *edit1;
    30. QLineEdit *edit2;
    31. private:
    32. Ui::Widget *ui;
    33. };
    34. #endif // WIDGET_H

     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. }

    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. }

    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. btn1=new QPushButton(QIcon("C:\\Users\\wuhuiwu\\Desktop\\login.png"),"登录",this);
    10. //设置固定长度
    11. this->setFixedSize(400,300);
    12. //设置窗口标题
    13. this->setWindowTitle("Widget");
    14. //设置窗口图标
    15. this->setWindowIcon(QIcon("C:\\Users\\wuhuiwu\\Desktop\\wodepeizhenshi.png"));
    16. //设置窗口尺寸
    17. btn1->setFixedSize(70,40);
    18. //移动按钮
    19. btn1->move(150,250);
    20. //再构造一个按钮,并给定父组件,图标,文本内容
    21. btn2=new QPushButton(QIcon("C:\\Users\\wuhuiwu\\Desktop\\cancel.png"),"取消",this);
    22. //设置按钮尺寸
    23. btn2->setFixedSize(70,40);
    24. //移动按钮
    25. btn2->move(btn1->x()+70,btn1->y());
    26. //实例化一个标签并指定父组件
    27. lab1=new QLabel(this);
    28. //设置尺寸
    29. lab1->resize(400,150);
    30. //设置图片
    31. lab1->setPixmap(QPixmap("C:\\Users\\wuhuiwu\\Desktop\\logo.png"));
    32. lab1->setScaledContents(true);
    33. //实例化一个标签并指定父组件
    34. lab2=new QLabel(this);
    35. //设置图片
    36. lab2->setPixmap(QPixmap("C:\\Users\\wuhuiwu\\Desktop\\userName.jpg"));
    37. //设置尺寸
    38. lab2->resize(40,30);
    39. //引动图标
    40. lab2->move(120,160);
    41. lab2->setScaledContents(true);
    42. //实例化一个标签并指定父组件
    43. lab3=new QLabel(this);
    44. //设置图片
    45. lab3->setPixmap(QPixmap("C:\\Users\\wuhuiwu\\Desktop\\passwd.jpg"));
    46. //设置尺寸
    47. lab3->resize(40,30);
    48. //移动图标
    49. lab3->move(lab2->x(),lab2->y()+50);
    50. lab3->setScaledContents(true);
    51. //构建一个文本编辑器
    52. edit1=new QLineEdit(this);
    53. //设置尺寸
    54. edit1->resize(100,30);
    55. edit1->move(lab2->x()+60,lab2->y());
    56. //设置占位文本
    57. edit1->setPlaceholderText("账号名");
    58. //构建一个文本编辑器
    59. edit2=new QLineEdit(this);
    60. //设置尺寸
    61. edit2->resize(100,30);
    62. edit2->move(edit1->x(),edit1->y()+50);
    63. //设置回显模式
    64. edit2->setEchoMode(QLineEdit::Password);
    65. edit1->setMaxLength(6);
    66. //登录
    67. connect(this->btn1,&QPushButton::clicked,this,&Widget::userLogin);
    68. //退出
    69. connect(this->btn2,&QPushButton::clicked,this,&Widget::userExit);
    70. }
    71. Widget::~Widget()
    72. {
    73. delete ui;
    74. }
    75. void Widget::userLogin()
    76. {
    77. QString accout=this->edit1->text();
    78. QString password=this->edit2->text();
    79. if(accout=="admin"&&password=="123456")
    80. {
    81. qDebug()<<"匹配成功";
    82. QMessageBox box(QMessageBox::NoIcon,"success","登录成功",
    83. QMessageBox::Ok);
    84. int res=box.exec();
    85. if(res==QMessageBox::Ok)
    86. {
    87. this->close();
    88. emit mysignals();
    89. }
    90. }else
    91. {
    92. qDebug()<<"账户密码不匹配,是否重新登录";
    93. QMessageBox box1(QMessageBox::Critical,"error","账户密码错误",
    94. QMessageBox::Ok|QMessageBox::Cancel);
    95. int res=box1.exec();
    96. if(res==QMessageBox::Ok)
    97. {
    98. this->edit2->clear();
    99. }
    100. else if(res==QMessageBox::Cancel)
    101. {
    102. this->close();
    103. }
    104. }
    105. }
    106. void Widget::userExit()
    107. {
    108. QMessageBox box2(QMessageBox::Warning,"退出","要退出吗?",
    109. QMessageBox::Yes|QMessageBox::No);
    110. int res=box2.exec();
    111. if(res==QMessageBox::Yes)
    112. {
    113. this->close();
    114. }
    115. else if(res==QMessageBox::No)
    116. {
    117. this->edit1->clear();
    118. this->edit2->clear();
    119. }
    120. }

     思维导图:

  • 相关阅读:
    一文详解,数据仓库、数据库、数据中台、数据湖的区别
    从小白到架构师(4): Feed 流系统实战
    sas小笔记-字符函数/时间函数
    代码规范 & 详细解释 husky、prettier、eslint、lint-staged 的作用和使用
    终于有人把面试必考的动态规划、链表、二叉树、字符串全部撸完了
    JavaWeb—会话技术
    Linux 完整 Redis 安装配置教程(可用远程连接)
    基于java+springmvc+mybatis+vue+mysql的水果食品果蔬生鲜商城销售系统
    Flink中的数据倾斜与解决方案实践
    MySQL存储引擎:选择合适的引擎优化数据库性能
  • 原文地址:https://blog.csdn.net/yjh666jhy/article/details/132991978