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 macro.

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<QTimer *>(obj);

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

  1. RTTI : Run-time type information
    #include <iostream>
    #include <typeinfo> 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 }
  2. 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<std::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 <QMainWindow> 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<QDebug>
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<QPushButton *>(sender()); ui->textLabel->setText(button->text()); if(ui->leftButton == button)
{
qDebug()<<"left Button clicked";
ui->textLabel->setStyleSheet("background-color:yellow");
button->setStyleSheet("background-color:yellow"); }
else
{
ui->textLabel->setStyleSheet("background-color:green");
button->setStyleSheet("background-color:green");
} }

完整的代码已上传Github

QT槽函数获取信号发送对象的更多相关文章

  1. PyQt学习随笔:槽函数获取信号发送对象的方法

    在PyQt中,相似控件发送的信号可以每个控件信号对应一个槽函数,也可以将相似控件的相同信号对应到一个槽函数,但如果槽函数相同,怎么区分信号是谁发送的呢?那就是在信号函数中使用sender()函数获取信 ...

  2. pyqt重写键盘事件+获取信号发送对象

    # _*_ coding:utf-8 _*_ import sys from PyQt4 import QtGui,QtCore class Example(QtGui.QMainWindow): d ...

  3. qt槽函数中,窗口镶嵌窗口的问题,求解

    my_label=newQLabel(ui->widget); my_Label->setText("yvhvv"); 我把这插入到构造函数中,正确显示. 我把这插入到 ...

  4. QT槽函数处理线程

    今天用到QTcpSocket的时候,由于客户端发起请求在一个线程里,当readyRead的信号发出后接收的槽函数是否还会在该线程里处理? 现在看来其实是交给主线程处理的,那么我要实现在线程里处理怎么实 ...

  5. pyqt的多Button的点击事件的槽函数的区分发送signal的按钮。

    关键函数:QPushButton的setObjectName()/objectName() 个人注解:按功能或者区域,将按钮的点击事件绑定的不同的槽函数上. from PyQt5.QtWidgets ...

  6. Qt 槽函数的使用

    今天在代码中遇到这样一个问题,自己感觉槽和函数都写的没错,但是就是不执行槽函数,因为是一个定时器的使用,即定时时间到了就执行槽函数. SeventhWizardPage::SeventhWizardP ...

  7. Qt槽函数创建

    法一 手动添加 private slots: void on_cancel_clicked(); void Widget::on_cancel_clicked() { } connect(ui-> ...

  8. PyQt(Python+Qt)学习随笔:Qt Designer中建立CommandLinkButton信号与Action的槽函数连接

    在Qt Designer中,通过F4进行信号和槽函数连接编辑时,接收信号的对象不能是Action对象,但在右侧的编辑界面,可以选择将一个界面对象的信号与Action对象的槽函数连接起来. 如图: 上图 ...

  9. 2.QT-窗口组件(QWidget),QT坐标系统,初探消息处理(信号与槽)

    本章主要内容如下: 1) 窗口组件(QWidget) 2) QT坐标系统 3) 消息处理(信号与槽) 窗口组件(QWidget) 介绍 Qt以组件对象的方式构建图形用户界面 Qt中没有父组件的顶级组件 ...

随机推荐

  1. Linux学习(一):常用命令

    init 0:关机 init 3:命令行模式 init 5:图形界面模式 init 6:重启 shutdown -h now:立马关机 ls:文件列表 参数:-l 详细列表 cd:切换目录 用法实例: ...

  2. Redis详解(十)------ 从零开始搭建集群

    在上一篇博客我们介绍了------Redis哨兵(Sentinel)模式,哨兵模式主要是解决高可用问题,在master节点宕机时,slave节点能够自动切换成为master节点 本篇博客我们来介绍Re ...

  3. Java IO(五)字节流 FileInputStream 和 FileOutputStream

    Java IO(五)字节流 FileInputStream 和 FileOutputStream 一.介绍 字节流 InputStream 和 OutputStream 是字节输入流和字节输出流的超类 ...

  4. 获取元素节点 & 操作属性节点

    1.html 文档编写 js 代码的位置: window.onload事件在整个html文档被完全加载完再执行,    所以可以获取html文档的任何节点 js-window-onload.html ...

  5. 基于Python豆瓣自动化测试【2】

    -- Pytest基础使用教程[2] 从测试报告说起 承接上一篇中最后的测试结果图,使用过的pytest-html 插件原生态的报告的话.可能会发现 内容样式都有些不同.其实是做了些扩展相关设置所呈现 ...

  6. Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics)部分(A~E)题解

    (A) Even Subset Sum Problem 题解:因为n非常非常小,直接暴力枚举所有区间即可. #include<bits/stdc++.h> using namespace ...

  7. Beta冲刺——凡事预则立

    这个作业属于哪个课程 软件工程 这个作业要求在哪里 Beta冲刺 这个作业的目标 Beta冲刺 作业正文 正文 github链接 项目地址 其他参考文献 无 1.讨论组长是否重选的议题和结论 经过讨论 ...

  8. IC-二进制, 自然数, 有符号数

    https://mp.weixin.qq.com/s/9rGs9yN-OvLvEclnDZr87Q   零. 概念和结论 1. 数:概念名,指代物体的多少,即物体的量的概念: 2. 值:概念属性,数的 ...

  9. 面试官问我会不会Elasticsearch,我语塞了...

    少点代码,多点头发 本文已经收录至我的GitHub,欢迎大家踊跃star 和 issues. https://github.com/midou-tech/articles 从今天开始准备给大家带来全新 ...

  10. (Java实现) 蓝桥杯 国赛 重复模式

    标题:重复模式 作为 drd 的好朋友,技术男 atm 在 drd 生日时送给他一个超长字符串 S .atm 要 drd 在其中找出一个最长的字符串 T ,使得 T 在 S 中至少出现了两次,而他想说 ...