-1

I have a class ("controller" for example) and in this class, I have created many objects of different other classes with different parents.

How to send signal between that classes and "controller" to call a function in "controller" class?

    #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;
    }

For example, how to send signal from "recivedCall" in each "PanelManager" object to call "alert" function in "controller" class ?

1 Answer 1

1

The object which creates your two components has to set the connections between your signal and your slot. But, you shouldn't expose inner components (i.e. create getter to return a pointer on a attribute).

A way to tackle the last problem with Qt is to create a signal in your parent and let it broadcast the calls. For example, if I need to connect a QCheckBox to a QLineEdit in two different widgets:

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);

In this example, when I click on the checkbox in parent1, the lineEdit in parent2 is disabled. But, Parent1 doesn't know anything about Parent2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.