题目:点击按钮改变文字
需要增加一个count属性,并且只需要定义槽,信号函数已经内置好了
//widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget //窗体对象
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
int count; //计数
public slots:
void pushBtn(void); //接收槽
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
//widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
count=0; //初始化count
//绑定信号与接收槽
connect(ui->pushButton_3,SIGNAL(clicked()),this,SLOT(pushBtn()));
connect(ui->pushButton_4,SIGNAL(clicked()),this,SLOT(pushBtn()));
}
Widget::~Widget()
{
delete ui;
}
void Widget::pushBtn()
{
count++;
// qDebug()<<"count="<<count;
QString str=QString("你点击了%1次").arg(count); // 类似printf
ui->label->setText(str);
}