【Qt 学习笔记】按钮实现helloworld | 信号与槽概述


  • 博客主页:Duck Bro 博客主页
  • 系列专栏:Qt 专栏
  • 关注博主,后期持续更新系列文章
  • 如果有错误感谢请大家批评指出,及时修改
  • 感谢大家点赞👍收藏⭐评论✍

按钮实现helloworld | 初识信号与槽

文章编号:Qt 学习笔记 / 09


在这里插入图片描述

一、图形化实现helloworld

1. 实现步骤

  1. 创建一个Qt项目,参考文章《使用QtCreator创建及运行项目 | 项目初始代码解释》

  2. 打开widget.ui文件,将Push Button控件拖拽至界面,并对按钮进行编辑内容
    在这里插入图片描述

  3. 引入信号槽connect
    在这里插入图片描述

在这里插入图片描述

  1. 编辑handleClick函数,识别按钮中文本是helloworld时点击切换成helloqt,反之则切换,函数代码如下
void Widget::handleClick()
{

    if(ui->pushButton->text()== QString("helloworld"))
    {
        qDebug() <<"helloqt";
        ui->pushButton->setText("helloqt");
    }
    else
    {
        qDebug() <<"helloworld";
        ui->pushButton->setText("helloworld");
    }

}
  1. 点击运行,即可

2. 代码演示

//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();
    void handleClick();

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);
    connect(ui->pushButton,&QPushButton::clicked,this,&Widget::handleClick);
}

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

void Widget::handleClick()
{

    if(ui->pushButton->text()== QString("helloworld"))
    {
        qDebug() <<"helloqt";
        ui->pushButton->setText("helloqt");
    }
    else
    {
        qDebug() <<"helloworld";
        ui->pushButton->setText("helloworld");
    }

}

二、纯代码实现helloworld

1. 实现步骤

  1. 创建一个Qt项目,参考文章《使用QtCreator创建及运行项目 | 项目初始代码解释》

  2. 打开widget.cpp文件,建立一个PushButton控件,在widget.h文件中将QPushButton* button设为全局变量

//widget.h
private:
    Ui::Widget *ui;
    QPushButton* button;
//widget.cpp
button=new QPushButton(this);
  1. 编辑按钮内的文字
button->setText("helloworld");
  1. 进行信号连接
connect(button,&QPushButton::clicked,this,&Widget::handleClick);
  1. 编辑handleClick函数,识别按钮中文本是helloworld时点击切换成helloqt,反之则切换,函数代码如下
void Widget::handleClick()
{
    if(button->text()==QString("helloworld"))
    {
        button->setText("helloqt");
    }
    else
    {
        button->setText("helloworld");
    }
}
  1. 点击运行,即可

2. 代码演示

//widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QPushButton>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    void handleClick();

private:
    Ui::Widget *ui;
    QPushButton* button;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    button=new QPushButton(this);
    button->setText("helloworld");
    connect(button,&QPushButton::clicked,this,&Widget::handleClick);
}

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

void Widget::handleClick()
{
    if(button->text()==QString("helloworld"))
    {
        button->setText("helloqt");
    }
    else
    {
        button->setText("helloworld");
    }
}

三、信号与槽

在上面的代码中出现connect

connect(button,&QPushButton::clicked,this,&Widget::handleClick);

在这里简单的提到一下Qt中信号与槽这个概念,在后续文章会对这方面内容进行详细的解释

1. 信号与槽概述

在Qt中,信号和槽是一种用于对象间通信的机制。它允许一个对象(发送者)通过发出信号,通知其他对象(接收者或槽函数)某个事件已经发生,而接收者则通过连接到信号的槽函数来响应这个事件。

以下是信号和槽的一些概述:

  1. 信号:信号是一种特殊的成员函数,用于通知其他对象某个事件已经发生。它们由关键字“signals”声明,并以特定格式定义。信号可以没有参数,也可以带有参数。

  2. 槽:槽是一种普通成员函数,用于接收信号,并执行一些相应的操作。槽函数可以有任意数量的参数,但必须与信号的参数列表匹配。槽函数可以被重载。

  3. 连接:连接是将信号与槽函数关联起来的过程。连接意味着当信号被发出时,相应的槽函数将被自动调用。可以使用QObject类的connect函数来建立连接。

  4. 发送信号:可以通过在对象中调用信号函数,来发出特定的信号。当信号被发出时,与之相连接的槽函数将被调用。

  5. 自定义信号和槽:在Qt中,可以自定义信号和槽来实现特定的功能。通过将自定义信号和槽函数添加到自定义的QObject派生类中,可以实现对象间的自定义通信。


在这里插入图片描述

  • 16
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Duck Bro

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值