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. How to emit signal dynamically

How to emit signal dynamically

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 1.1k 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.
  • L Offline
    L Offline
    Luis Ortiz
    wrote on last edited by
    #1

    Hello, everyone.

    In the project I am developing, a requirement came up to update values for different modules (classes).
    I am developing a class which will translate the value and its module name and emit a signal that is connected to a slot in the specified module.
    The only problem is: how can I emit a specific signal for each model without the need of hardcoded code?
    There's a example of how I am currently implementing it.

    Header example:

    #define MODULE_NAME_1 "Module1"
    
    public slots:
       void onValueUpdate(QString moduleName, int value);
    signals:
        void notifyToModule1(int value);
        void notifyToModule2(int value);
        ....
    

    Cpp example:

    void ValueUpdate(QString moduleName, int value)
    {
        if(notifyToModule1.contains(moduleName))
        {
            emit notifyToModule1(value);
        }
        if(notifyToModule2.contains(moduleName))
        {
            emit notifyToModule2(value);
        }
        ....
    }
    

    There was a solution: Use an unique signal that is connected to each module's slots. Then, each slot would verify if its name equals the parameter "moduleName" and discard the value if not. However, this implementation could cause each slot to be called every 1 ms and I can't see this being okay.
    Still, in my opinion, this implementation is very ugly. Therefore, I would like some thoughts about how I can do this differently by using any kind of design pattern or Qt feature.

    J.HilkJ 1 Reply Last reply
    0
    • L Luis Ortiz

      Hello, everyone.

      In the project I am developing, a requirement came up to update values for different modules (classes).
      I am developing a class which will translate the value and its module name and emit a signal that is connected to a slot in the specified module.
      The only problem is: how can I emit a specific signal for each model without the need of hardcoded code?
      There's a example of how I am currently implementing it.

      Header example:

      #define MODULE_NAME_1 "Module1"
      
      public slots:
         void onValueUpdate(QString moduleName, int value);
      signals:
          void notifyToModule1(int value);
          void notifyToModule2(int value);
          ....
      

      Cpp example:

      void ValueUpdate(QString moduleName, int value)
      {
          if(notifyToModule1.contains(moduleName))
          {
              emit notifyToModule1(value);
          }
          if(notifyToModule2.contains(moduleName))
          {
              emit notifyToModule2(value);
          }
          ....
      }
      

      There was a solution: Use an unique signal that is connected to each module's slots. Then, each slot would verify if its name equals the parameter "moduleName" and discard the value if not. However, this implementation could cause each slot to be called every 1 ms and I can't see this being okay.
      Still, in my opinion, this implementation is very ugly. Therefore, I would like some thoughts about how I can do this differently by using any kind of design pattern or Qt feature.

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @Luis-Ortiz
      you'll have to branch eventually,
      either before emitting the signal or in the connected slots.

      What you could do is replace the module name with an enum, Integer comparing is much faster than string


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      5
      • M Offline
        M Offline
        mpergand
        wrote on last edited by mpergand
        #3

        The classic way to prevent branching is to use a array ( or map ) of class pointers, each module implementing the same interface ( notifyToModule ) like this:

        QMap(QString, ModuleInterface*)
        and simply call:
        map[ModuleName]->notifyToModule(value);

        In modern c++ (>= C++11) I think it should be possible to store function pointers or even lambda for each module.

        L 1 Reply Last reply
        2
        • M mpergand

          The classic way to prevent branching is to use a array ( or map ) of class pointers, each module implementing the same interface ( notifyToModule ) like this:

          QMap(QString, ModuleInterface*)
          and simply call:
          map[ModuleName]->notifyToModule(value);

          In modern c++ (>= C++11) I think it should be possible to store function pointers or even lambda for each module.

          L Offline
          L Offline
          Luis Ortiz
          wrote on last edited by
          #4

          @mpergand That is exactly what I was looking for. Such a simple implementation. I don't know how I was not able to think about this by myself.
          Actually, trying to use Qt features exclusively may be the problem as I keep forgetting about C++ features.
          Thank you very much!

          @J-Hilk That is what I thought. I will use the @mpergand instead. Thanks!

          Pablo J. RoginaP 1 Reply Last reply
          0
          • L Luis Ortiz

            @mpergand That is exactly what I was looking for. Such a simple implementation. I don't know how I was not able to think about this by myself.
            Actually, trying to use Qt features exclusively may be the problem as I keep forgetting about C++ features.
            Thank you very much!

            @J-Hilk That is what I thought. I will use the @mpergand instead. Thanks!

            Pablo J. RoginaP Offline
            Pablo J. RoginaP Offline
            Pablo J. Rogina
            wrote on last edited by
            #5

            @Luis-Ortiz said in How to emit signal dynamically:

            I will use the @mpergand instead

            Could your issue be called as solved? If so, please don't forget to mark your post as such.

            Upvote the answer(s) that helped you solve the issue
            Use "Topic Tools" button to mark your post as Solved
            Add screenshots via postimage.org
            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • M mpergand referenced this topic on

            • Login

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