Important: Please read the Qt Code of Conduct -

Connecting Sibling Slots based on Signal Values

  • Hi there,

    I am busy trying to figure out the best practice for coding, regarding the following:

    I have 1 sender object that generates signals and another vector of receiver objects at the same level in the hierarchy.

    I am trying to connect a signal between the two in such a way that the signal itself contains the index of the receiver object in the vector.

    Some Pseudocode will help better explain... in the parent source code I want something along the lines of:

    QObject::connect(ChildSenderObject, SIGNAL(valueChanged(index, value)),
    ChildReceiverObjectsVector[i], SLOT(changeValue(index, value)));

    where the vector index i here is the value of index argument of SIGNAL

    Currently the only solution I can think of off the top of my head is to connect them via a parent set of signals and slots like so:

    QObject::connect(ChildSenderObject, SIGNAL(valueChanged(index, value)),
    this, SLOT(findChildObject(index,value)));

    with the accompanying slot function:

    parentObject::findChildObject(index i,value v){

    So I guess my question is this: Should I be trying to connect them directly to each other or should I connect them through the parent? What is the best practice? If I can connect them directly without using a parent member function, then how exactly would I do this? Do I use a signal mapper?

  • Lifetime Qt Champion

    @Anony-Mouse Your first approach is not a good solution: you would need to connect each object from the vector to the signal, but then all objects would get the signal and would then have to check the index to decide whether they should handle the signal or not.
    The second solution is better.

Log in to reply