需求:点击Button按钮(下班),label显示淦饭
信号发出者:Button
发送的信号(signals):被点击
信号接收者:label
信号的处理(slots):显示淦饭
分析:因为这信号是按键被点击,故信号不需要自定义
槽函数实现将label输出淦饭即可
一、项目创建
项目名称:Afterwork
二、UI布局
一个button,一个label
三、Afterwork.h
声明槽函数void Show()
进行设置lable显示
#pragma once#include <QtWidgets/QMainWindow>
#include "ui_Afterwork.h"class Afterwork : public QMainWindow
{Q_OBJECTpublic:Afterwork(QWidget *parent = nullptr);~Afterwork();
public slots:void Show();
private:Ui::AfterworkClass ui;
};
四、Afterwork.cpp
槽函数void Show()
的实现
ui.label->setText("eat food!!!");
ui里面的label的name为label,故ui.label
为lable对象
将label的text设置为eat food!!!
void Afterwork::Show()
{qDebug() << "after work we will to eat more food!!!";ui.label->setText("eat food!!!");
}
关联
connect(ui.pushButton,&QPushButton::clicked,this,&Afterwork::Show);
信号发出者为:ui.pushButton
发送的信号为:&QPushButton::clicked按键被点击
信号接收者为:this当前这个QWidget
信号的处理(槽函数):&Afterwork::Show
Afterwork.cpp
完整代码:
#include "Afterwork.h"
#include <QDebug>
Afterwork::Afterwork(QWidget *parent): QMainWindow(parent)
{ui.setupUi(this);connect(ui.pushButton,&QPushButton::clicked,this,&Afterwork::Show);
}Afterwork::~Afterwork()
{}void Afterwork::Show()
{qDebug() << "after work we will to eat more food!!!";ui.label->setText("eat food!!!");
}
五、效果演示
项目运行
点击按钮