Invoke slot based on meta-type id of parameter


  • Qt Champions 2016

    Hello,
    This is a sort of continuation of my previous thread. Now, supposedly I've been able to serialize/deserialize my messages (haven't tested it yet, but I will very soon). After all the shenanigans with the meta-type system this is the last piece of the puzzle. So the problem is as follows:

    1. When I receive a message from a node the corresponding QMpiNode object emits a signal void messageReceived(const QMpiMessage & message, QMpiNode * peer);
    2. This signal has to be processed somewhere in the user code.
    3. The user provides the QObject that processes the signal and the correct subclass of QMpiMessage (the actual object is created of the correct type through the meta-object system).

    Suppose that the user's QObject has a couple of slots that should handle two different message types:

    MyMessageHandler::processMessage(const MyFirstMessage & message, QMpiNode * peer)
    {
        // ...
    }
    
    MyMessageHandler::processMessage(const MySecondMessage & message, QMpiNode * peer)
    {
        // ...
    }
    

    The question is how to connect the signal emitted from QMpiNode, i.e. void QMpiNode::messageReceived(const QMpiMessage & message, QMpiNode * peer) to invoke the correct slot in MyMessageHandler by making the distinction only based on the object I have behind the QMpiMessage & reference?
    I hope it's clear what I'm asking.

    Kind regards.


  • Moderators

    I would give MyMessageHandler only 1 slot. Inside this slot, query the QMpiMessage to discover its subclass, and then call the appropriate function to process it.

    @kshegunov said:

    The question is how to connect the signal emitted from QMpiNode, i.e. void QMpiNode::messageReceived(const QMpiMessage & message, QMpiNode * peer) to invoke the correct slot in MyMessageHandler by making the distinction only based on the object I have behind the QMpiMessage & reference?
    I hope it's clear what I'm asking.

    That's a bit like asking "How to connect to QSlider::valueChanged(int), and invoke different slots depending on whether the value is odd or even?" The answer is: You can't. Slots are invoked based on the sender object ID and the signal ID, but not the signal parameter values.

    In your description, you only have 1 signal. All slots connected to that signal will be invoked each time the signal is emitted. Emitting a signal that carries a different subclass of QMpiMessage is just like emitting a signal that carries a different number. If you want to invoke different slots, then you need to emit different signals.


  • Qt Champions 2016

    @JKSH

    I would give MyMessageHandler only 1 slot. Inside this slot, query the QMpiMessage to discover its subclass, and then call the appropriate function to process it.

    I was more interested in the meta-object magic I have to perform, to call the appropriate function, but I think I'd found how to do it.

    That's a bit like asking "How to connect to QSlider::valueChanged(int), and invoke different slots depending on whether the value is odd or even?" The answer is: You can't. Slots are invoked based on the sender object ID and the signal ID, but not the signal parameter values.
    In your description, you only have 1 signal. All slots connected to that signal will be invoked each time the signal is emitted. Emitting a signal that carries a different subclass of QMpiMessage is just like emitting a signal that carries a different number. If you want to invoke different slots, then you need to emit different signals.

    Fair enough. I admit I've used "connect" and "slot" a bit frivolously, so I probably need to put more effort in writing clearer questions. In any case, your comment I believe pointed me in the right direction, so I thank you for that!

    Kind regards.


Log in to reply
 

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