• DAY51


     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 "form.h"
    12. #include
    13. QT_BEGIN_NAMESPACE
    14. namespace Ui { class Widget; }
    15. QT_END_NAMESPACE
    16. class Widget : public QWidget
    17. {
    18. Q_OBJECT
    19. public:
    20. Widget(QWidget *parent = nullptr);
    21. ~Widget();
    22. private slots:
    23. void on_btn2_clicked(QLineEdit * edit1,QLineEdit * edit2);
    24. void on_btn1_clicked();
    25. void on_btn3_clicked();
    26. signals:
    27. void jump();
    28. private:
    29. Ui::Widget *ui;
    30. Form *s1;
    31. Form *th;
    32. };
    33. #endif // WIDGET_H

    form.h

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

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

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

    widget.cpp

    1. #include "widget.h"
    2. #include "ui_widget.h"
    3. Widget::Widget(QWidget *parent)
    4. : QWidget(parent)
    5. {
    6. ui->setupUi(this);
    7. s1 = new Form;
    8. connect(this,&Widget::jump,s1,&Form::jump_slot);
    9. qDebug() << this->size(); //获取当前界面的尺寸
    10. this->resize(QSize(500,400)); //重新使用匿名对象作为函数参数设置当前组件的尺寸
    11. this->setMaximumSize(1000,800); //设置最大尺寸
    12. this->setMinimumSize(200,100); //设置最小尺寸
    13. this->setFixedSize(600,400); //设置固定尺寸
    14. qDebug() << this->windowTitle(); //获取当前组件的窗口标题
    15. this->setWindowTitle("221#223社畜登录框"); //设置窗口标题
    16. this->setWindowIcon(QIcon("C:\\Users\\Administrator\\Desktop\\QQ.png")); //设置窗口图标
    17. this->setStyleSheet("background-color:skyblue;"); //设置窗口样式表
    18. this->setWindowOpacity(1); //设置窗口透明度
    19. //this->setWindowFlag(Qt::FramelessWindowHint); //去掉窗口头部
    20. //使用无参构造函数,构造一个按钮
    21. QPushButton *btn1 = new QPushButton;
    22. //btn1->show(); //有该函数,但是没有父组件,只能单独存在
    23. btn1->setParent(this); //将当前界面作为父组件
    24. btn1->setText("找回密码"); //设置按钮上的文本内容
    25. qDebug()<size(); //获取按钮尺寸
    26. btn1->resize(80,30); //重新设置按钮组件尺寸
    27. qDebug()<size();
    28. btn1->setIcon(QIcon("C:\\Users\\Administrator\\Desktop\\QQ.png")); //设置图标
    29. btn1->move(150,350); //移动按钮组件
    30. btn1->setStyleSheet("background-color:skyblue; border-radius:10px; "); //设置按钮样式表
    31. //btn1->setEnabled(false); //设置不可用状态
    32. //构造一个按钮,构造是直接指定父组件
    33. QPushButton *btn2 = new QPushButton(this);
    34. btn2->setText("登录");
    35. btn2->resize(btn1->size());
    36. btn2->move(btn1->x() + 110,btn1->y());
    37. btn2->setIcon(QIcon("C:\\Users\\Administrator\\Desktop\\QQ.png"));
    38. btn2->setStyleSheet("background-color:skyblue; border-radius:10px; ");
    39. //btn2->setEnabled(false);
    40. //构造按钮时直接给定文本内容以及父组件
    41. QPushButton *btn3 = new QPushButton(this);
    42. btn3->resize(btn1->size());
    43. btn3->setText("退出");
    44. btn3->move(btn2->x() + 110,btn2->y());
    45. btn3->setIcon(QIcon("C:\\Users\\Administrator\\Desktop\\QQ.png"));
    46. btn3->setStyleSheet("background-color:skyblue; border-radius:10px; ");
    47. //btn3->setEnabled(false);
    48. /*
    49. //构造按钮时直接给定图标、文本内容、父组件
    50. QPushButton *btn4 = new QPushButton(QIcon("C:\\Users\\Administrator\\Desktop\\QQ.png"),"选项",this);
    51. btn4->move(btn3->x() + 150,btn3->y());
    52. btn4->setStyleSheet("background-color:skyblue; border-radius:10px; ");
    53. btn4->setEnabled(false);
    54. */
    55. //构造一个行编辑器对象,并指定父组件
    56. QLineEdit *edit1 = new QLineEdit(this);
    57. edit1->resize(250,30);
    58. edit1->move(btn1->x() + 50,btn1->y() - 150);
    59. edit1->setStyleSheet("background-color:skyblue;");
    60. //edit1->setEchoMode(QLineEdit::Password); //设置回显模式
    61. edit1->setMaxLength(10);
    62. edit1->setPlaceholderText("社畜账号/手机/邮箱");
    63. //edit1->setText("*******"); //设置默认文本
    64. //edit1->setAlignment(Qt::AlignCenter); //文本内容居中
    65. //QComboBox *cb1 = new QComboBox(this);
    66. //构造一个行编辑器,给定初始文本,并指定父组件
    67. //QLineEdit *edit2 = new QLineEdit("*****",this);
    68. QLineEdit *edit2 = new QLineEdit(this);
    69. edit2->resize(250,30);
    70. edit2->move(btn1->x() + 50,btn1->y() - 100);
    71. edit2->setStyleSheet("background-color:skyblue;");
    72. edit2->setEchoMode(QLineEdit::Password); //设置回显模式
    73. edit2->setMaxLength(15);
    74. edit2->setPlaceholderText("******");
    75. //实例化一个标签,并给定初始文本内容,并指定父组件
    76. QLabel *lab1 = new QLabel("账号:",this);
    77. lab1->resize(40,30);
    78. lab1->setStyleSheet("background-color:skyblue;");
    79. lab1->move(btn1->x(),btn1->y() - 150);
    80. QLabel *lab2 = new QLabel("密码:",this);
    81. lab2->resize(40,30);
    82. lab2->setStyleSheet("background-color:skyblue;");
    83. lab2->move(btn1->x(),btn1->y() - 100);
    84. QLabel *lab3 = new QLabel(this);
    85. lab3->resize(600,200);
    86. lab3->setStyleSheet("background-color:white;");
    87. lab3->move(0,btn1->y() - 400);
    88. lab3->setPixmap(QPixmap("C:\\Users\\Administrator\\Desktop\\QQ截图20230915200445.png"));
    89. lab3->setScaledContents(true);
    90. QCheckBox *chbox1 = new QCheckBox("自动登录",this);
    91. chbox1->move(btn1->x() + 40,btn1->y() - 50);
    92. QCheckBox *chbox2 = new QCheckBox("记住密码",this);
    93. chbox2->move(btn1->x() + 180,btn1->y() - 50);
    94. }
    95. Widget::~Widget()
    96. {
    97. delete ui;
    98. }
    99. void Widget::on_btn2_clicked(QLineEdit * edit1,QLineEdit * edit2)
    100. {
    101. //登录失败
    102. if(edit1->text()!="admin"&&edit2->text()!="123456")
    103. {
    104. //调用构造函数实例化对象
    105. QMessageBox box(QMessageBox::Critical,//图标
    106. "错误",//对话框标题
    107. "账号密码不匹配,是否重新登录",//对话框文本
    108. QMessageBox::Yes|QMessageBox::No,//提供的按钮
    109. this);//父组件
    110. box.setDefaultButton(QMessageBox::Yes);
    111. box.setButtonText(QMessageBox::Yes,"OK");
    112. box.setButtonText(QMessageBox::No,"cancel");
    113. //调用exec函数运行对话框
    114. int ret = box.exec();
    115. //对结果进行判断
    116. if(ret==QMessageBox::Yes)
    117. {
    118. //清空内容
    119. edit1->clear();
    120. }
    121. else
    122. {
    123. //关闭界面
    124. this->close();
    125. }
    126. }
    127. //登录成功
    128. else
    129. {
    130. //调用构造函数实例化对象
    131. QMessageBox box(QMessageBox::NoIcon,//图标
    132. " ",//对话框标题
    133. "登录成功",//对话框文本
    134. QMessageBox::Ok,//提供的按钮
    135. this);//父组件
    136. box.exec();//运行对话框
    137. emit jump();//跳转界面
    138. this->close();//关闭当前界面
    139. }
    140. }
    141. //退出按钮
    142. void Widget::on_btn3_clicked()
    143. {
    144. int ret=QMessageBox::warning(this,
    145. "警告",
    146. "是否确定要退出登录",
    147. QMessageBox::Yes | QMessageBox::No,
    148. QMessageBox::No);
    149. //对结果进行判断如果点yes则关闭
    150. if(ret==QMessageBox::Yes)
    151. {
    152. this->close();//关闭界面
    153. }
    154. }
    155. void Widget::on_btn1_clicked()
    156. {
    157. }

  • 相关阅读:
    文心一言 VS 讯飞星火 VS chatgpt (94)-- 算法导论9.2 3题
    Basic Facilities of a Virtio Device (一)
    口袋参谋:如何利用“问大家”机制,提高转化?
    【吴恩达老师《机器学习》】课后习题5之【偏差与方差】
    LVS-DR
    Python复现颜色图绘制大赛的作品
    南大通用数据库-Gbase-8a-学习-19-Gbase8a从Kafka订阅Topic消费数据
    Zabbix监控系统系列之二十二:ESXi虚拟化监控
    2022年C++面试题万余字汇总【面试官常问】
    数据备份管理中的分类定级:方法、标准与策略
  • 原文地址:https://blog.csdn.net/weixin_52839062/article/details/132997534