Signal from grand child class to grand parent slot



  • Hi!
    What is the commonly used and most easy way using signal-slot between objects which do not have access to each other? For example:

    class a{
    signals:
      void action();
    };
    
    class b{
    private:
      a objA;
    };
    
    class c{
    private:
      b objB;
    private slots:
      void onAction();
    }
    

    and class C need know about a::action() emit.


  • Moderators

    @qxoz
    the most straight forward way is using signals. If the classes are authored by you, you might want to make them friend-classes thus you can access the private members and do the connection directly. Otherwise you have to pass the pointer to the receiver around of course.

    But this can be solved in multiple ways, e.g. when you already design your code smartly.


  • Moderators

    @qxoz If I understand you correctly you want to connect action() from c::objB.objA to onAction() in c, right? Since objA is private in b you cannot do it in c. If you want to connect a signal to a slot you need access to both instances.



  • Thank you for answers!
    Is there other way let know about the event? Maybe without signal-slot mechanism.


  • Moderators

    @qxoz
    depends what you actually want to achieve. You simply provided too less info to give a optimal solution.

    1. you could handle up the parent chain and check for some properties/object-name/etc.
    2. post a custom event to the event loop and let the anyone listening to it receive it
    3. ...


  • What I would do is use a bridging signal:

    class a : QObject{
    Q_OBJECT
    signals:
      void action();
    };
    
    class b : QObject{
    Q_OBJECT
    public:
    b(){
    connect(objA,&a::action,this,&b::action);
    }
    private:
      a objA;
    signals:
      void action();
    };
    
    class c : QObject{
    Q_OBJECT
    private:
      b objB;
    private slots:
      void onAction();
    }
    


  • @VRonin
    This is the first that came to my mind. But i thought is not the good way.

    @raven-worx said in Signal from grand child class to grand parent slot:

    post a custom event to the event loop and let the anyone listening to it receive it

    Probably this is the good solution.
    And i know, problems like this, are result of bad architecture.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.