• QTday02(常用类、UI界面下的开发、信号与槽)


    今日任务

    1.

    使用手动连接,将登录框中的取消按钮使用qt4版本的连接到自定义的槽函数中,在自定义的槽函数中调用关闭函数

    将登录按钮使用qt5版本的连接到自定义的槽函数中,在槽函数中判断ui界面上输入的账号是否为"admin",密码是否为"123456",如果账号密码匹配成功,则输出“登录成功”,并关闭该界面,如果匹配失败,则输出登录失败,并将密码框中的内容清空

    2.思维导图

    功能代码:

    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(560,430);
    9. this->setStyleSheet("background-color:#faf7ec");
    10. this->setWindowFlag(Qt::FramelessWindowHint);//无边框
    11. QMovie *movie = new QMovie(":/111/cai.gif");
    12. ui->backLabel->setMovie(movie);
    13. ui->backLabel->setScaledContents(true);
    14. movie->start();
    15. ui->closeButton->setStyleSheet("border-image:url(:/111/basketball.png)");
    16. ui->avatorLabel->resize(60,60);
    17. ui->avatorLabel->setStyleSheet("border-image:url(:/111/user.png);border-radius:30px");
    18. ui->accountLabel->setPixmap(QPixmap(":/111/account.jpg"));
    19. //ui->accountLabel->resize(40,40);
    20. ui->accountLabel->setScaledContents(true);
    21. ui->passwdLabel->setPixmap(QPixmap(":/111/passwd.jpg"));
    22. //ui->passwdLabel->resize(40,40);
    23. ui->passwdLabel->setScaledContents(true);
    24. ui->accoountLine->setPlaceholderText("账号");
    25. ui->passwdLine->setPlaceholderText("密码");
    26. ui->passwdLine->setEchoMode(QLineEdit::Password);
    27. ui->loginLabel->setPixmap(QPixmap(":/111/2.png"));
    28. ui->loginLabel->setScaledContents(true);
    29. ui->loginButton->setStyleSheet("background-color:#409EFF;border-radius:5px");
    30. connect(ui->closeButton,SIGNAL(clicked()),this,SLOT(close()));
    31. connect(ui->loginButton,&QPushButton::clicked,this,&Widget::loginButton_slot);
    32. }
    33. Widget::~Widget()
    34. {
    35. delete ui;
    36. }
    37. void Widget::loginButton_slot()
    38. {
    39. if(ui->accoountLine->text()=="admin"&&ui->passwdLine->text()=="123456"){
    40. qDebug() << "登录成功" <
    41. this->close();
    42. }else{
    43. qDebug() << "账号或者密码错误" <
    44. ui->passwdLine->setText("");
    45. }
    46. }

    头文件:

    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include
    4. #include
    5. #include
    6. QT_BEGIN_NAMESPACE
    7. namespace Ui { class Widget; }
    8. QT_END_NAMESPACE
    9. class Widget : public QWidget
    10. {
    11. Q_OBJECT
    12. public:
    13. Widget(QWidget *parent = nullptr);
    14. ~Widget();
    15. public slots:
    16. void loginButton_slot();
    17. private:
    18. Ui::Widget *ui;
    19. };
    20. #endif // WIDGET_H

    UI界面布局:

    运行结果:

    今日思维导图:

    课程代码:参考一下可以

    头文件:

    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include
    4. #include
    5. #include //语音播报类
    6. QT_BEGIN_NAMESPACE
    7. namespace Ui { class Widget; }
    8. QT_END_NAMESPACE
    9. class Widget : public QWidget
    10. {
    11. Q_OBJECT //有关信号和槽的宏
    12. public:
    13. Widget(QWidget *parent = nullptr);
    14. ~Widget();
    15. signals: //表示该权限下都是信号函数
    16. void my_signal(); //自定义一个信号函数
    17. public slots: //表示该权限下是公共的槽函数
    18. void my_slot(); //自定义一个槽函数
    19. void Btn4_slot(); //btn4对应的槽函数声明
    20. private slots:
    21. void on_Btn2_clicked(); //系统定义的槽函数声明
    22. void on_Btn6_clicked(); //按钮6对应槽函数的声明
    23. void on_Btn7_clicked();
    24. private:
    25. Ui::Widget *ui;
    26. QPushButton *btn3; //定义一个按钮3
    27. //定义一个语音播报者
    28. QTextToSpeech *speecher;
    29. };
    30. #endif // WIDGET_H

    源文件

    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. speecher = new QTextToSpeech(this);
    10. //实例化一个按钮
    11. btn3 = new QPushButton("按钮3",this);
    12. btn3->move(ui->Btn2->x(), ui->Btn2->y()+ui->Btn2->height()+10);
    13. btn3->resize(ui->Btn2->width(),ui->Btn2->height());
    14. //手动连接信号和系统槽,基于qt4版本 是不友好的连接
    15. //函数原型:[static] QMetaObject::Connection QObject::connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type = Qt::AutoConnection)
    16. //参数1:是一个组件的指针 信号的发送者
    17. //参数2:信号函数,需要宏函数转换
    18. //参数3:是一个组件的指针 信号的接受者
    19. //参数4:槽函数,需要宏函数转换
    20. //connect(btn3, SIGNAL(clicked()), this, SLOT(close()));
    21. //手动连接信号和系统槽,基于qt4版本 是不友好的连接
    22. this->connect(btn3, SIGNAL(clicked()), this,SLOT(my_slot()));
    23. //手动连接信号和自定义槽,基于qt5版本 是友好的连接
    24. //函数原型:[static] QMetaObject::Connection QObject::connect(const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type = Qt::AutoConnection)
    25. //参数1:是一个组件的指针 信号的发送者
    26. connect(ui->Btn4, &QPushButton::clicked, this, &Widget::Btn4_slot);
    27. //手动连接信号和lambda表达式
    28. connect(ui->Btn5, &QPushButton::clicked, [&](){
    29. ui->Btn5->setText("55555");
    30. });
    31. //手动连接自定义的信号
    32. connect(this, &Widget::my_signal,[&](){
    33. ui->Btn6->resize(ui->Btn6->width()+5, ui->Btn6->height()+5);
    34. });
    35. }
    36. Widget::~Widget()
    37. {
    38. delete ui;
    39. }
    40. //自定义槽函数的实现
    41. void Widget::my_slot()
    42. {
    43. this->close();
    44. }
    45. //按钮4对应的槽函数实现 #include //语音播报类
    46. void Widget::Btn4_slot()
    47. {
    48. //this->close();
    49. static int num = 0;
    50. if(num%3 == 0)
    51. {
    52. speecher->say("咳咳咳");
    53. }
    54. else if (num%3 == 1)
    55. {
    56. speecher->say(ui->Btn2->text());
    57. }
    58. else if (num%3 == 2)
    59. {
    60. speecher->say(this->btn3->text());
    61. }
    62. ++num;
    63. }
    64. //按钮2对应的槽函数
    65. void Widget::on_Btn2_clicked()
    66. {
    67. //让按钮1变色
    68. //ui->Btn1->setStyleSheet("background-color:red");
    69. static int num = 0;
    70. if(num%3 == 0)
    71. {
    72. ui->Btn1->setStyleSheet("background-color:red");
    73. }
    74. else if (num%3 == 1)
    75. {
    76. ui->Btn1->setStyleSheet("background-color:green");
    77. }
    78. else if (num%3 == 2)
    79. {
    80. ui->Btn1->setStyleSheet("background-color:blue");
    81. }
    82. ++num;
    83. }
    84. //按钮6对应的槽函数处理
    85. void Widget::on_Btn6_clicked()
    86. {
    87. emit my_signal(); //触发(发射)自定义的信号
    88. }
    89. void Widget::on_Btn7_clicked()
    90. {
    91. //断开连接
    92. disconnect(ui->Btn4, &QPushButton::clicked, this, &Widget::Btn4_slot);
    93. }

  • 相关阅读:
    JavaEE中常见的锁策略
    Java反应式编程(1)
    Java基础实战项目-------网上订餐系统
    通达OA RCE远程代码执行漏洞分析
    基于Spring Boot+ Vue的健身房管理系统与实现
    Android 12.0 Launcher3 去掉Hotseat功能
    【C++】list的介绍及使用 | 模拟实现list(万字详解)
    STM32Cube串口中断只执行一次问题
    Python开发技术—文件和异常2
    ZPL II 语言编程基础
  • 原文地址:https://blog.csdn.net/weixin_53762703/article/details/133912495