QT槽函数获取信号发送对象
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会增加程序运行时间,这里简单记录区分一下 。
- 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 }
- 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槽函数获取信号发送对象的更多相关文章
- PyQt学习随笔:槽函数获取信号发送对象的方法
在PyQt中,相似控件发送的信号可以每个控件信号对应一个槽函数,也可以将相似控件的相同信号对应到一个槽函数,但如果槽函数相同,怎么区分信号是谁发送的呢?那就是在信号函数中使用sender()函数获取信 ...
- pyqt重写键盘事件+获取信号发送对象
# _*_ coding:utf-8 _*_ import sys from PyQt4 import QtGui,QtCore class Example(QtGui.QMainWindow): d ...
- qt槽函数中,窗口镶嵌窗口的问题,求解
my_label=newQLabel(ui->widget); my_Label->setText("yvhvv"); 我把这插入到构造函数中,正确显示. 我把这插入到 ...
- QT槽函数处理线程
今天用到QTcpSocket的时候,由于客户端发起请求在一个线程里,当readyRead的信号发出后接收的槽函数是否还会在该线程里处理? 现在看来其实是交给主线程处理的,那么我要实现在线程里处理怎么实 ...
- pyqt的多Button的点击事件的槽函数的区分发送signal的按钮。
关键函数:QPushButton的setObjectName()/objectName() 个人注解:按功能或者区域,将按钮的点击事件绑定的不同的槽函数上. from PyQt5.QtWidgets ...
- Qt 槽函数的使用
今天在代码中遇到这样一个问题,自己感觉槽和函数都写的没错,但是就是不执行槽函数,因为是一个定时器的使用,即定时时间到了就执行槽函数. SeventhWizardPage::SeventhWizardP ...
- Qt槽函数创建
法一 手动添加 private slots: void on_cancel_clicked(); void Widget::on_cancel_clicked() { } connect(ui-> ...
- PyQt(Python+Qt)学习随笔:Qt Designer中建立CommandLinkButton信号与Action的槽函数连接
在Qt Designer中,通过F4进行信号和槽函数连接编辑时,接收信号的对象不能是Action对象,但在右侧的编辑界面,可以选择将一个界面对象的信号与Action对象的槽函数连接起来. 如图: 上图 ...
- 2.QT-窗口组件(QWidget),QT坐标系统,初探消息处理(信号与槽)
本章主要内容如下: 1) 窗口组件(QWidget) 2) QT坐标系统 3) 消息处理(信号与槽) 窗口组件(QWidget) 介绍 Qt以组件对象的方式构建图形用户界面 Qt中没有父组件的顶级组件 ...
随机推荐
- 小谢第8问:ui框架的css样式如何更改
目前有三种方法, 1.使用scss,增加样式覆盖,但是此种方法要求css的className需要与框架内的元素相一致,因此修改时候需要特别注意,一个父级的不同就可能修改失败 2.deep穿透,这种方法 ...
- day07 作业
作业(必做题):#1. 使用while循环输出1 2 3 4 5 6 8 9 10count=0while count<11: if count==7: count+=1 continue pr ...
- 【Hadoop】namenode与secondarynamenode的checkpoint合并元数据
Checkpoint Node(检查点节点) NameNode persists its namespace using two files: fsimage, which is the latest ...
- Java实现 蓝桥杯VIP 基础练习 芯片测试
问题描述 有n(2≤n≤20)块芯片,有好有坏,已知好芯片比坏芯片多. 每个芯片都能用来测试其他芯片.用好芯片测试其他芯片时,能正确给出被测试芯片是好还是坏.而用坏芯片测试其他芯片时,会随机给出好或是 ...
- Java实现WUST 1002: 哈夫曼树
[问题描述] 根据给定的若干权值可以构造出一颗哈夫曼树.构造的哈夫曼树可能不唯一,但是按照下面的选取原则所构造出来的哈夫曼树应该是唯一的. (1)每次选取优先级最低的两个结点,优先级最低的作为左子树, ...
- Java实现 LeetCode 16 最接近的三数之和
16. 最接近的三数之和 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存 ...
- 第七届蓝桥杯JavaA组国(决)赛部分真题
解题代码部分来自网友,如果有不对的地方,欢迎各位大佬评论 题目1.阶乘位数 阶乘位数 9的阶乘等于:362880 它的二进制表示为:1011000100110000000 这个数字共有19位. 请你计 ...
- 自己动手写SQL执行引擎
自己动手写SQL执行引擎 前言 在阅读了大量关于数据库的资料后,笔者情不自禁产生了一个造数据库轮子的想法.来验证一下自己对于数据库底层原理的掌握是否牢靠.在笔者的github中给这个database起 ...
- PAT 在霍格沃茨找零钱
如果你是哈利·波特迷,你会知道魔法世界有它自己的货币系统 —— 就如海格告诉哈利的:“十七个银西可(Sickle)兑一个加隆(Galleon),二十九个纳特(Knut)兑一个西可,很容易.”现在,给定 ...
- zabbix 监控进程,端口
环境介绍 操作系统:centos 7.4 zabbix版本:zabbix server 3.4.7 客户端:zabbix-agent 3.4.7 监控进程:mysqld 监控端口:3306 tcp 进 ...