槽函数获取sender_QT槽函数获取信号发送对象

本文介绍了在Qt中如何在槽函数中利用`sender()`获取信号发送对象,详细解释了`qobject_cast`函数的用法,并通过一个示例展示了如何根据信号源做出不同响应。同时,文章提到了RTTI(运行时类型信息)和RAII(资源获取即初始化)的概念,以帮助读者理解相关知识。
摘要由CSDN通过智能技术生成

Qt 在槽函数中获取信号发送对象

Qt中提供了一个函数 qobject_cast(QObject *object),可以通过这个函数判断信号发出对象

Qt 帮助文档的解释:

Returns the given object cast to type T if the object is of type T (or of a subclass); otherwise returns nullptr. If object is nullptr then it will also return nullptr.

The class T must inherit (directly or indirectly) QObject and be declared with the Q_OBJECT mac++ro.

A class is considered to inherit itself.

The qobject_cast() function behaves similarly to the standard C++ dynamic_cast(), with the advantages that it doesn’t require RTTI support and it works across dynamic library boundaries.

简单来说 就是使用这个函数会得到你转型的的一个实例,但是这个类必须继承自QObject 或者其子类并且声明Q_OBJECT 这个宏

QT 帮助文档中一个Example

QObject *obj = new QTimer;

QTimer *timer = qobject_cast(obj);

这里顺便记录一下RTTI 与RAII,RAII 通常应用于对象资源管理,RTTI 可以动态判断对象类型,但使用RTTI会增加程序运行时间,这里简单记录区分一下 。

RTTI : Run-time type information

#include

#include

class Base {

public:

virtual ~Base() = default;

};

class Derived : public Base {};

int main() {

Base base;

Derived derived;

Base* ptr = &derived;

Base& ref = derived;

std::cout << typeid(base).name()<< std::endl; // class Base

std::cout << typeid(derived).name()<< std::endl; // class Derived

std::cout << typeid(ptr).name()<< std::endl; // class Base *

std::cout << typeid(*ptr).name() << std::endl; //class Derived

std::cout << typeid(ref).name() << std::endl; //class Derived

}

RAII : Resource Acquisition Is Initialization

代码来源 https://en.cppreference.com/w/cpp/language/raii

std::mutex m;

void bad()

{

m.lock(); // acquire the mutex

f(); // if f() throws an exception, the mutex is never released

if(!everything_ok()) return; // early return, the mutex is never released

m.unlock(); // if bad() reaches this statement, the mutex is released

}

void good()

{

std::lock_guard<:mutex> lk(m); // RAII class: mutex acquisition is initialization

f(); // if f() throws an exception, the mutex is released

if(!everything_ok()) return; // early return, the mutex is released

}

下面是QT通过qobject_cast获取信号发送对象的一个Demo,通过Qt Desinger 绘制两个按钮和一个文本框,将两个按钮的点击事件连接到同一个槽函数,在槽函数里面判断信号的发送者并作出不同的响应

主要的代码如下:

#ifndef MAINWINDOW_H

#define MAINWINDOW_H

#include

QT_BEGIN_NAMESPACE

namespace Ui { class MainWindow; }

QT_END_NAMESPACE

class MainWindow : public QMainWindow

{

Q_OBJECT

public:

MainWindow(QWidget *parent = nullptr);

~MainWindow();

public slots:

void onButtonClicked();

private:

Ui::MainWindow *ui;

};

#endif // MAINWINDOW_H

#include "mainwindow.h"

#include "ui_mainwindow.h"

#include

MainWindow::MainWindow(QWidget *parent)

: QMainWindow(parent)

, ui(new Ui::MainWindow)

{

ui->setupUi(this);

connect(ui->leftButton,&QPushButton::clicked,this,&MainWindow::onButtonClicked);

connect(ui->rightButton,&QPushButton::clicked,this,&MainWindow::onButtonClicked);

}

MainWindow::~MainWindow()

{

delete ui;

}

void MainWindow::onButtonClicked()

{

QPushButton *button = qobject_cast(sender());

ui->textLabel->setText(button->text());

if(ui->leftButton == button)

{

qDebug()<

ui->textLabel->setStyleSheet("background-color:yellow");

button->setStyleSheet("background-color:yellow");

}

else

{

ui->textLabel->setStyleSheet("background-color:green");

button->setStyleSheet("background-color:green");

}

}

完整的代码已上传Github

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值