Qt信号和槽绑定实例,点击pushbutton按钮触发QLabel文本显示和关闭

功能简介:

点击qt界面中的open按钮在界面中显示文本,同时按钮变为close按钮,再点击close按钮可以关闭显示文本,同时按钮变为open按钮,继续点击open按钮。。。。。。

关键内容:

// 关联按钮按事件和信号识别槽
connect(this->displayCtrlBtn, &QPushButton::clicked, this, &HelloWorld::DisplayControl);

1.创建一个qt工程

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    helloworld.cpp

HEADERS += \
    helloworld.h

FORMS += \
    helloworld.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

2.定义一个QPuschButton按钮实例和一个QLabel文本显示实例

#ifndef HELLOWORLD_H
#define HELLOWORLD_H

#include <QWidget>
#include <QPushButton>
#include <QLabel>

QT_BEGIN_NAMESPACE
namespace Ui { class HelloWorld; }
QT_END_NAMESPACE

class HelloWorld : public QWidget
{
    Q_OBJECT

public:
    HelloWorld(QWidget *parent = nullptr);
    ~HelloWorld();
    void DisplayControl();

private slots:

private:
    Ui::HelloWorld *ui;
    QPushButton *displayCtrlBtn;
    QLabel *helloWroldLabel;
    bool labelStatus;
};
#endif // HELLOWORLD_H

3.设置按钮显示内容和文本框显示内容, 关联按钮按事件和信号识别槽

#include "helloworld.h"
#include "ui_helloworld.h"
#include <QDebug>
#include <QPushButton>

HelloWorld::HelloWorld(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::HelloWorld)
{
    ui->setupUi(this);
    labelStatus = true;
    displayCtrlBtn = new QPushButton;
    displayCtrlBtn->setParent(this);//将按钮挂载到窗口上
    displayCtrlBtn->setText("Open");
    displayCtrlBtn->move(250, 250);

    helloWroldLabel = new QLabel(this);
    this->helloWroldLabel->setText("hello world!");
    // 设置坐标和大小
    helloWroldLabel->setGeometry(180, 50, 400, 200);
    // 设置字号大小
    QFont ft;
    ft.setPointSize(18);
    helloWroldLabel->setFont(ft);
    // 设置颜色
    QPalette pale;
    pale.setColor(QPalette::WindowText, Qt::red);
    helloWroldLabel->setPalette(pale);
    helloWroldLabel->hide();
    // 关联按钮按事件和信号识别槽
    connect(this->displayCtrlBtn, &QPushButton::clicked, this, &HelloWorld::DisplayControl);
}

HelloWorld::~HelloWorld()
{
    delete ui;
}

void HelloWorld::DisplayControl()
{
    qDebug() << "my_btn_callback !  " << endl;
    if (labelStatus) {
        helloWroldLabel->hide();
        this->displayCtrlBtn->setText("Open");
        labelStatus = false;
    } else {
        helloWroldLabel->show();
        this->displayCtrlBtn->setText("close");
        labelStatus = true;
    }
}

4.main函数中添加测试

#include "helloworld.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    HelloWorld w;
    w.show();
    return a.exec();
}

5.运行效果

打开时界面中没有显示文本,需要点击open按钮显示文本内容

点击open按钮显示文本,同时open按钮变为close按钮,再点击close按钮关闭显示文本以及修改按钮状态

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值