与不同父母的班级中的对象之间发送信号
send signal between objects in a class with different parents
我有一个类(例如"控制器")在此课程中,我创建了许多其他类别的对象 与不同的父母。
如何在该类和"控制器"之间发送信号以在"控制器"类中调用函数?
#include "controller.h"
Controller::Controller(QObject *parent) : QObject (parent){
connect(sender(), SIGNAL(recivedCall(QString)), this, SLOT(alert(QString)));
}
void Controller::onCall(QJsonObject callinfo){
nodes[callinfo["username"].toString()]= new PanelManager();
nodes[callinfo["username"].toString()]->handleCallStateChanged(callinfo);
}
void Controller::alert(QString callinfo){
qDebug()<<callinfo;
}
例如,如何在每个" panelmanager"对象中从"控制器"类中调用"警报"函数中的"再生"信号?
创建两个组件的对象必须在信号和插槽之间设置连接。但是,您不应该揭露内部组件(即创建getter以返回属性上的指针)。
解决QT最后一个问题的一种方法是在您的父母中创建信号并让其广播呼叫。例如,如果我需要将QCheckBox连接到两个不同小部件的Qlineedit:
class Parent1: public QWidget
{
Q_OBJECT
public:
Parent1(): QWidget(), myCheckBox(new QCheckBox("Edit", this))
{
connect(myCheckBox, &QCheckBox::clicked, this, &Parent1::editForbidden);
}
private:
QCheckBox* myCheckBox;
signals:
void editForbidden(bool);
};
class Parent2: public QWidget
{
Q_OBJECT
public:
Parent2(): QWidget(), myLineEdit(new QLineEdit("Edit", this))
{
connect(this, &Parent2::forbidEdit, myLineEdit, &QLineEdit::setReadOnly);
}
private:
QLineEdit* myLineEdit;
signals:
void forbidEdit(bool);
};
// In the parent of Parent1 and Parent2 (or in the main if there is no parent)
QObject::connect(p1, &Parent1::editForbidden, p2, &Parent2::forbidEdit);
在此示例中,当我单击parent1
中的复选框时,禁用了parent2
中的LINEDIT。但是,parent1对parent2不了解。
相关文章:
- 线程之间的布尔停止信号
- 通过插槽和信号在不同线程中的两个qt对象之间进行通信
- 父进程和子进程之间的 POSIX 信号量
- 在信号槽机制与传统环路之间做出决定
- C/C++ 在条件和阻止呼叫之间收到信号情报
- 使用两个任务之间的静音信号
- 如何将信号连接到2个共享库之间的插槽
- 在QT C 中使用QMOVIE和信号/插槽之间在GIF中导航
- 连接类实例和另一个类实例中的对话框之间的槽和信号
- 不同类别的 2 个对象之间的信号/插槽
- 两个对象QT之间的示例插槽/信号
- 与不同父母的班级中的对象之间发送信号
- QT和Libevent中的信号和插槽之间的差异
- 为什么我的数据无法在信号/插槽的表格之间传输
- 在带有QMovie的GIF动画和Qt中的信号/插槽之间切换
- 在Qt中的线程之间发送对象作为信号参数
- 信号和插槽之间的2个窗口QT
- 尝试在Qt中连接隐藏信号的两种方式之间做出决定
- 无法在多个 Qthread的 QThread 和 finish() 信号之间连接
- 锁、内存屏障、信号量之间的区别