Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Invoke slot based on meta-type id of parameter

Invoke slot based on meta-type id of parameter

Scheduled Pinned Locked Moved Solved General and Desktop
qmetatypeqmetaobject
3 Posts 2 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • kshegunovK Offline
    kshegunovK Offline
    kshegunov
    Moderators
    wrote on last edited by
    #1

    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.

    Read and abide by the Qt Code of Conduct

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by JKSH
      #2

      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 Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      kshegunovK 1 Reply Last reply
      3
      • JKSHJ 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.

        @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.

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #3

        @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.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved