• QT day3


    widget.h

    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include
    4. #include
    5. QT_BEGIN_NAMESPACE
    6. namespace Ui { class Widget; }
    7. QT_END_NAMESPACE
    8. class Widget : public QWidget
    9. {
    10. Q_OBJECT
    11. public:
    12. Widget(QWidget *parent = nullptr);
    13. ~Widget();
    14. signals:
    15. void jump();//自定义一个跳转信号
    16. private slots:
    17. void on_loginbtn_clicked();
    18. void on_cancelbtn_clicked();
    19. private:
    20. Ui::Widget *ui;
    21. };
    22. #endif // WIDGET_H

    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:
    11. explicit second(QWidget *parent = nullptr);
    12. ~second();
    13. public slots:
    14. void jumpSlot(); //对应第一个窗口信号的槽函数声明
    15. private:
    16. Ui::second *ui;
    17. };
    18. #endif // SECOND_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. //实例化第二个窗口的对象
    10. second s;
    11. //将一个窗口的jump信号和第二窗口的jumpSlot槽函数连接
    12. QObject::connect(&w, &Widget::jump, &s, &second::jumpSlot);
    13. return a.exec();
    14. }

    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. this->setWindowTitle("QQ");
    10. this->setWindowIcon(QIcon(":/pictrue/qq.png"));
    11. //标签的相关设置
    12. ui->logolabel->setPixmap(QPixmap(":/pictrue/logo.png"));
    13. ui->logolabel->setScaledContents(true);
    14. //账号和密码
    15. ui->usernamelabel->resize(35,35);
    16. ui->usernamelabel->setPixmap(QPixmap(":/pictrue/userName.jpg"));
    17. ui->usernamelabel->setScaledContents(true);
    18. ui->passwordlabel->resize(35,35);
    19. ui->passwordlabel->setPixmap(QPixmap(":/pictrue/passwd.jpg"));
    20. ui->passwordlabel->setScaledContents(true);
    21. ui->usernamelineEdit->setPlaceholderText("QQ账号/手机号码/邮箱");
    22. // ui->usernamelineEdit->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
    23. // ui->usernamelineEdit->adjustSize();
    24. ui->passwordlineEdit->setEchoMode(QLineEdit::Password);
    25. //登录按钮和取消登录按钮
    26. ui->loginbtn->setIcon(QIcon(":/pictrue/login.png"));
    27. ui->cancelbtn->setIcon(QIcon(":/pictrue/cancel.png"));
    28. //qt5版本 判断账号是否为admin,密码是否为123456
    29. connect(ui->loginbtn,&QPushButton::clicked,this,&Widget::on_loginbtn_clicked);
    30. }
    31. Widget::~Widget()
    32. {
    33. delete ui;
    34. }
    35. //登录按钮对应的槽函数
    36. void Widget::on_loginbtn_clicked()
    37. {
    38. if(ui->usernamelineEdit->text()=="admin" && ui->passwordlineEdit->text()=="123456")
    39. {
    40. QMessageBox msg(
    41. QMessageBox::Information,//图标
    42. "信息对话框", //消息对话框标题
    43. "登录成功", //对话框文本
    44. QMessageBox::Ok, //提供OK按钮
    45. this); //父对象
    46. //用exec()函数执行对话框
    47. int ret = msg.exec();
    48. //对用户选中的按钮进行判断
    49. if(ret == QMessageBox::Ok)
    50. {
    51. this->close();
    52. emit jump();
    53. }
    54. }
    55. else
    56. {
    57. QMessageBox msg(
    58. QMessageBox::Critical, //错误图标
    59. "错误对话框", //错误对话框标题
    60. "账号和密码不匹配", //错误对话框文本
    61. QMessageBox::Yes | QMessageBox::No, //提供Yes/No按钮
    62. this); //父对象
    63. //用exec()函数执行对话框
    64. int ret = msg.exec();
    65. //对用户选中的按钮进行判断
    66. if(ret == QMessageBox::Yes)
    67. {
    68. //清除密码框中的内容
    69. ui->passwordlineEdit->setText("");
    70. }
    71. else if(ret == QMessageBox::No)
    72. {
    73. //关闭登录
    74. this->close();
    75. }
    76. }
    77. }
    78. //取消登录按钮对应的槽函数
    79. void Widget::on_cancelbtn_clicked()
    80. {
    81. //直接调用静态成员函数
    82. int ret = QMessageBox::question(this,
    83. "问题对话框",
    84. "您是否确定要退出登录?",
    85. QMessageBox::Yes | QMessageBox::No);
    86. //对用户选中的按钮进行判断
    87. if(ret == QMessageBox::Yes)
    88. {
    89. this->close();
    90. }
    91. else if(ret == QMessageBox::No)
    92. {
    93. }
    94. }

    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::jumpSlot()
    14. {
    15. this->show();
    16. }

  • 相关阅读:
    多线程(二)多线程的锁机制(java)
    vue中 mock 使用及注意事项
    RuoYi-Vue Spring Security 密码加密
    手撕520页PDF高级文档,成功“挤掉”7年开发架构师,牛逼
    【FLY】Android内存性能优化
    Three---面向对象与面向过程/属性和变量/关于self/一些魔法方法的使用/继承/super方法/多态
    AIE有机荧光探针/荧光高分子纳米微球AIE-PEN FPNs/AIE有机荧光分子带电荷微球
    基于模糊预测与扩展卡尔曼滤波的野值剔除方法
    【前端笔试】知识点总结2
    【并发编程】并发工具类
  • 原文地址:https://blog.csdn.net/qq_51722235/article/details/133935243