与不同父母的班级中的对象之间发送信号

send signal between objects in a class with different parents

本文关键字:之间 信号 对象 同父母      更新时间:2023-10-16

我有一个类(例如"控制器")在此课程中,我创建了许多其他类别的对象 与不同的父母。

如何在该类和"控制器"之间发送信号以在"控制器"类中调用函数?

    #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不了解。