• C++&QT 作业9


     widget.h

    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include "second.h"
    10. QT_BEGIN_NAMESPACE
    11. namespace Ui {class Widget;}
    12. QT_END_NAMESPACE
    13. class Widget : public QWidget
    14. {
    15. Q_OBJECT
    16. public:
    17. Widget(QWidget *parent = nullptr);
    18. ~Widget();
    19. signals:
    20. void btn1_signal();
    21. public slots:
    22. void btn1_slot();
    23. void btn2_slot();
    24. private:
    25. Ui::Widget *ui;
    26. QPushButton *btn1;
    27. QPushButton *btn2;
    28. QLineEdit *edit1;
    29. QLineEdit *edit2;
    30. QLabel *lab1;
    31. QLabel *lab2;
    32. QLabel *lab3;
    33. Second *s1;
    34. };
    35. #endif

    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. explicit Second(QWidget *parent = nullptr);
    13. ~Second();
    14. public slots:
    15. void jump_slot1();
    16. private:
    17. Ui::Second *ui;
    18. };
    19. #endif

    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. return a.exec();
    10. }

    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. Second::~Second()
    10. {
    11. delete ui;
    12. }
    13. void Second::jump_slot1(){
    14. this->show();
    15. }

    widget.cpp

    1. #include "widget.h"
    2. #include "ui_widget.h"
    3. #include<QDebug>
    4. #include <QIcon>
    5. Widget::Widget(QWidget *parent)
    6. : QWidget(parent)
    7. ,ui(new Ui::Widget)
    8. {
    9. ui->setupUi(this);
    10. s1 = new Second;
    11. //将图形化界面的名字改成登录界面
    12. this->setWindowTitle("Login screen");
    13. //将图标改成自定义图片内容
    14. this->setWindowIcon(QIcon(":/icon/wodepeizhenshi.png"));
    15. //设置ui界面的大小为合适的大小
    16. this->setFixedSize(QSize(400,300));
    17. //插入一个图标
    18. lab1 = new QLabel(this);
    19. lab1->resize(QSize(400,125));
    20. lab1->move(0,0);
    21. //内容要是一张图片
    22. lab1->setPixmap(QPixmap(":/icon/logo.png"));
    23. //设置图片填充
    24. lab1->setScaledContents(true);
    25. //插入两个行编辑器
    26. edit1 = new QLineEdit(this);
    27. edit1->resize(QSize(240,40));
    28. edit1->move(110,150);
    29. edit1->setPlaceholderText("账号");
    30. //输入密码
    31. edit2 = new QLineEdit(this);
    32. edit2->resize(QSize(240,40));
    33. edit2->move(edit1->x(),edit1->y()+55);
    34. edit2->setPlaceholderText("密码");
    35. //回显模式是密码模式
    36. edit2->setEchoMode(QLineEdit::Password);
    37. lab2 = new QLabel(this);
    38. lab2->resize(50,40);
    39. lab2->setPixmap(QPixmap(":/icon/userName.jpg"));
    40. lab2->setScaledContents(true);
    41. lab2->move(edit1->x()-60,edit1->y());
    42. lab3 = new QLabel(this);
    43. lab3->resize(50,40);
    44. lab3->setPixmap(QPixmap(":/icon/passwd.jpg"));
    45. lab3->setScaledContents(true);
    46. lab3->move(edit2->x()-60,edit2->y());
    47. //两个按钮
    48. QPushButton *btn1 = new QPushButton("登录",this);
    49. QPushButton *btn2 = new QPushButton("取消",this);
    50. //重设尺寸
    51. btn1->resize(QSize(60,30));
    52. btn2->resize(btn1->size());
    53. //移动位置
    54. btn1->move(120,edit2->y()+55);
    55. btn2->move(btn1->x()+120,edit2->y()+55);
    56. btn1->setIcon(QIcon(":/icon/login.png"));
    57. btn2->setIcon(QIcon(":/icon/cancel.png"));
    58. //将两个按钮连到自己定义的槽函数中
    59. connect(btn1,&QPushButton::clicked,this,&Widget::btn1_slot);
    60. connect(btn2,&QPushButton::clicked,this,&Widget::btn2_slot);
    61. connect(this, &Widget::btn1_signal,s1,&Second::jump_slot1);
    62. }
    63. void Widget::btn1_slot(){
    64. QMessageBox msgbox;
    65. if(edit1->text()=="admin" && edit2->text()=="123456"){
    66. msgbox.setIcon(QMessageBox::NoIcon);
    67. msgbox.setWindowTitle("LOL");
    68. msgbox.setText("登录成功");
    69. msgbox.setStandardButtons(QMessageBox::Yes);
    70. msgbox.setDefaultButton(QMessageBox::Yes);
    71. int i = msgbox.exec();
    72. if(i == QMessageBox::Yes)
    73. this->close();
    74. }
    75. else{
    76. int ret;
    77. msgbox.setIcon(QMessageBox::Critical);
    78. msgbox.setWindowTitle("LOL");
    79. msgbox.setText("账号密码错误,是否重新登录?");
    80. msgbox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
    81. msgbox.setDefaultButton(QMessageBox::Ok);
    82. ret = msgbox.exec();
    83. if(ret == QMessageBox::Ok){
    84. edit2->clear();
    85. }
    86. else if(ret == QMessageBox::Cancel){
    87. this->close();
    88. }
    89. }
    90. }
    91. void Widget::btn2_slot(){
    92. int ret =QMessageBox::warning(
    93. this,
    94. "LOL",
    95. "是否退出?",
    96. QMessageBox::Yes|QMessageBox::No,
    97. QMessageBox::Yes);
    98. if(ret == QMessageBox::No){
    99. this->close();
    100. }
    101. }
    102. Widget::~Widget()
    103. {
    104. }

  • 相关阅读:
    JVM调优相关命令以及解释
    PhpSpreadsheet读写Excel文件
    [MapStruct]枚举的映射
    SMTP发送邮件时抱No appropriate protocol错误分析和解决方案
    尚硅谷最新Node.js 学习笔记(一)
    【毕业设计】大数据分析的客户细分 (聚类分析) - python k-means
    深入探讨Spring Boot:实现一个完整的RESTful应用程序
    微服务迁移、重构最佳经验
    想要好身材,你不得不看的高蛋白饮食计划
    Golang Map 基本原理
  • 原文地址:https://blog.csdn.net/weixin_55993382/article/details/133000292