• QT Day2


    Second.h

    1. #ifndef SECOND_H
    2. #define SECOND_H
    3. #include
    4. namespace Ui {
    5. class Second;
    6. }
    7. class Second : public QWidget
    8. {
    9. Q_OBJECT
    10. public slots:
    11. void jump_slot(); //接收跳转信号的槽函数
    12. public:
    13. explicit Second(QWidget *parent = nullptr);
    14. ~Second();
    15. private:
    16. Ui::Second *ui;
    17. };
    18. #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. #include
    10. #include"second.h"
    11. QT_BEGIN_NAMESPACE
    12. namespace Ui { class Widget; }
    13. QT_END_NAMESPACE
    14. class Widget : public QWidget
    15. {
    16. Q_OBJECT
    17. signals: //该权限下定义属于自己的信号
    18. void my_signal(); //自定义一个无参无返回值的信号函数
    19. void jump(); //自定义跳转的信号函数;
    20. private slots:
    21. void my_slot(); //自定义无参无返回值的槽函数
    22. void my_slot1();
    23. public:
    24. Widget(QWidget *parent = nullptr);
    25. ~Widget();
    26. private:
    27. Ui::Widget *ui;
    28. QLabel* lab1;
    29. QLabel* lab2;
    30. QLabel* lab3;
    31. QLineEdit* edit1;
    32. QLineEdit* edit2;
    33. QPushButton *btn1;
    34. QPushButton *btn2;
    35. void on_quesBtn_clicked();
    36. void on_critBtn_clicked();
    37. void on_infoBtn_clicked();
    38. };
    39. #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::jump,&s,&Second::jump_slot);
    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. Second::~Second()
    10. {
    11. delete ui;
    12. }
    13. //接收跳转信号对应的槽函数
    14. void Second::jump_slot()
    15. {
    16. this->show(); //
    17. }

    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. this->setFixedSize(800,600); //设置固定尺寸
    9. this->setWindowTitle("Widget"); //设置窗口标题
    10. this->setWindowIcon(QIcon(":/wodepeizhenshi.png")); //设置窗口图标
    11. //实例化标签
    12. lab1=new QLabel(this);
    13. lab1->setPixmap(QPixmap("D:\\qt23062\\day8\\logo.png")); //设置图片
    14. lab1->setScaledContents(true); //设置图片自动适应大小
    15. lab1->resize(800,270); //重新设置尺寸
    16. //标签2
    17. lab2=new QLabel(this);
    18. lab2->setPixmap(QPixmap(":/userName.jpg")); //设置图片
    19. lab2->setScaledContents(true); //设置图片自动适应大小
    20. lab2->resize(60,60); //重新设置尺寸
    21. lab2->move(200,300);
    22. //标签3
    23. lab3=new QLabel(this);
    24. lab3->setPixmap(QPixmap(":/passwd.jpg")); //设置图片
    25. lab3->setScaledContents(true); //设置图片自动适应大小
    26. lab3->resize(60,60); //重新设置尺寸
    27. lab3->move(lab2->x(),lab2->y()+90);
    28. //行编辑器
    29. edit1=new QLineEdit("",this);
    30. edit1->setPlaceholderText("账号");
    31. edit1->move(lab2->x()+75,lab2->y());
    32. edit1->resize(360,60);
    33. edit2=new QLineEdit("",this);
    34. edit2->setPlaceholderText("密码");
    35. edit2->move(lab3->x()+75,lab3->y());
    36. edit2->resize(360,60);
    37. edit2->setEchoMode(QLineEdit::Password); //设置回显模式
    38. //按钮
    39. btn1=new QPushButton(QIcon(":/login.png"),"登录",this);
    40. btn1->move(edit2->x()+50,edit2->y()+100);
    41. btn1->resize(110,70); //重新设置尺寸
    42. QFont font = btn1->font();
    43. font.setPointSize(16); // 设置字体大小为16像素
    44. btn1->setFont(font);
    45. QPushButton *btn2=new QPushButton(QIcon(":/cancel.png"),"取消",this);
    46. btn2->move(btn1->x()+150,btn1->y());
    47. btn2->resize(110,70); //重新设置尺寸
    48. QFont font1 = btn2->font();
    49. font1.setPointSize(16); // 设置字体大小为16像素
    50. btn2->setFont(font1);
    51. connect(btn1, &QPushButton::clicked, this, &Widget::my_slot);
    52. connect(btn2, &QPushButton::clicked, this, &Widget::my_slot1);
    53. connect(this,&Widget::my_signal,this,&Widget::close);
    54. }
    55. Widget::~Widget()
    56. {
    57. delete ui;
    58. }
    59. //槽函数
    60. void Widget::my_slot()
    61. {
    62. if(edit1->text()=="admin"&&edit2->text()=="123456")
    63. {
    64. //信息对话框
    65. on_infoBtn_clicked();
    66. }else
    67. {
    68. //错误对话框
    69. on_critBtn_clicked();
    70. }
    71. }
    72. void Widget::my_slot1()
    73. {
    74. //问题对话框
    75. on_quesBtn_clicked();
    76. }
    77. //问题对话框
    78. void Widget::on_quesBtn_clicked()
    79. {
    80. //1.调用构造函数实例化对象
    81. QMessageBox box(QMessageBox::Question, //图标
    82. "问题对话框", //对话框标题
    83. "是否确定要退出登入", //对话框文本内容
    84. QMessageBox::Yes|QMessageBox::No, //提供的按钮
    85. this); //父组件
    86. //box.setDetailedText("有上好的飞天茅台"); //提供详细文本内容
    87. //box.setDefaultButton(QMessageBox::No); //将no设置成默认按钮
    88. //2.调用exec函数运行对话框
    89. int ret=box.exec();
    90. //3.对结果进行判断
    91. if(ret==QMessageBox::Yes)
    92. {
    93. emit my_signal();
    94. }else if(ret==QMessageBox::No)
    95. {
    96. }
    97. }
    98. //信息对话框
    99. void Widget::on_infoBtn_clicked()
    100. {
    101. //直接调用静态成员函数完成对话框的实现
    102. int ret=QMessageBox::information(this, //父组件
    103. "信息对话框", //对话框标题
    104. "登入成功", //对话框文本内容
    105. QMessageBox::Ok, //对话框提供的按钮
    106. QMessageBox::Ok); //默认选中的按钮
    107. if (ret==QMessageBox::Ok)
    108. {
    109. emit my_signal();
    110. emit jump();
    111. }
    112. }
    113. //错误对话框
    114. void Widget::on_critBtn_clicked()
    115. {
    116. //1.调用构造函数实例化对象
    117. QMessageBox box(QMessageBox::Critical, //图标
    118. "错误对话框", //对话框标题
    119. "账号密码不匹配,是否重新登入", //对话框文本内容
    120. QMessageBox::Yes|QMessageBox::No, //提供的按钮
    121. this); //父组件
    122. //box.setDetailedText("账号密码不匹配,是否重新登入"); //提供详细文本内容
    123. //box.setDefaultButton(QMessageBox::No); //将no设置成默认按钮
    124. box.setButtonText(QMessageBox::Yes,"OK");
    125. box.setButtonText(QMessageBox::No,"cancel");
    126. //2.调用exec函数运行对话框
    127. int ret=box.exec();
    128. //3.对结果进行判断
    129. if(ret==QMessageBox::Yes)
    130. {
    131. edit2->clear();
    132. }else if(ret==QMessageBox::No)
    133. {
    134. emit my_signal();
    135. }
    136. }

  • 相关阅读:
    java 实现发送邮箱,复制即用,包含邮箱设置第三方登录授权码获取方法
    Javaweb之javascript的详细解析
    阿里云SLB之:基于HTTPS协议的SLB应用场景(十二)
    CS224W 6 Graph Neural Networks
    MAC地址震荡,STP震荡,OSPF路由协议震荡
    在基于乐鑫芯片的用户定制开发板上开发 UI
    【C++从0到王者】第三十七站:模拟unordered_map和unordered_set
    Java项目:ssm停车位租赁系统
    【模型部署】人脸检测模型DBFace C++ ONNXRuntime推理部署(0)
    Flask 学习-1.简介与环境准备
  • 原文地址:https://blog.csdn.net/weixin_58469613/article/details/132996673