Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Signal/slot and const parameters
Forum Updated to NodeBB v4.3 + New Features

Signal/slot and const parameters

Scheduled Pinned Locked Moved Solved C++ Gurus
11 Posts 5 Posters 5.9k Views 2 Watching
  • 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.
  • KroMignonK KroMignon

    Hi dear C++ gurus,

    I have perhaps a silly question about Signal / Slots connection.

    When I declare a signal or a slots in my class, is it a good or stupid idea to add "const &" to parameters?
    Do I have to add "const &" also to connected slots?

    Eg:

    class DataCollrecor: public QObject
    {
        Q_OBJECT
    
    ...
    signals:
        void newMessage(const MessageInfo& message);
    };
    
    class Listener : public QObject
    {
        Q_OBJECT
    
    ...
    public slots:
        void onNewMessage(const MessageInfo& message);
    };
    

    Is this a good or bad practice?

    Regards

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #2

    @KroMignon
    I'm not a C++ Guru, but I would say it is a good idea. If it's a const & then make it so (like you show, in both signal & slot). Qt in-built signals/slots do this (I don't know know if they do it everywhere, but they do do it).

    1 Reply Last reply
    1
    • KroMignonK KroMignon

      Hi dear C++ gurus,

      I have perhaps a silly question about Signal / Slots connection.

      When I declare a signal or a slots in my class, is it a good or stupid idea to add "const &" to parameters?
      Do I have to add "const &" also to connected slots?

      Eg:

      class DataCollrecor: public QObject
      {
          Q_OBJECT
      
      ...
      signals:
          void newMessage(const MessageInfo& message);
      };
      
      class Listener : public QObject
      {
          Q_OBJECT
      
      ...
      public slots:
          void onNewMessage(const MessageInfo& message);
      };
      

      Is this a good or bad practice?

      Regards

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by aha_1980
      #3

      @KroMignon said in Signal/slot and const parameters:

      When I declare a signal or a slots in my class, is it a good or stupid idea to add "const &" to parameters?

      Good idea for signals as well as slots :)

      (As long as the parameter has a size > sizeof(pointer)

      Qt has to stay free or it will die.

      KroMignonK 1 Reply Last reply
      3
      • aha_1980A aha_1980

        @KroMignon said in Signal/slot and const parameters:

        When I declare a signal or a slots in my class, is it a good or stupid idea to add "const &" to parameters?

        Good idea for signals as well as slots :)

        (As long as the parameter has a size > sizeof(pointer)

        KroMignonK Offline
        KroMignonK Offline
        KroMignon
        wrote on last edited by
        #4

        @aha_1980 Thanks for your attention, but does I need to have "const &" for each Signal and Slot or does it only made sense for Slots?

        And when I use QMetaObject::invokeMethod(), do I need to add "const &" in Q_ARG() ?

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #5

          It makes sense to add it in the declaration of both signals and slots. Qt will take care automatically of the lifecycle for arguments passed across threads.
          SGNAL() and SLOT() all support both the versions with and without const & but it's faster at execution if you leave it out.

          So following your example:

          • void newMessage(const MessageInfo& message);
          • void onNewMessage(const MessageInfo& message);
          • connect(collector,SIGNAL(newMessage(MessageInfo)),listener,SLOT(onNewMessage(MessageInfo));

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          5
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #6

            I also recommend declaring signals as const:

            signals:
                void newMessage(const MessageInfo& message) const;
            

            Every signal can be const (always), it may help compiler optimize stuff, and it allows you to emit a signal also from const methods. So you can win (a small bit) and don't loose anything.

            (Z(:^

            VRoninV 1 Reply Last reply
            2
            • sierdzioS sierdzio

              I also recommend declaring signals as const:

              signals:
                  void newMessage(const MessageInfo& message) const;
              

              Every signal can be const (always), it may help compiler optimize stuff, and it allows you to emit a signal also from const methods. So you can win (a small bit) and don't loose anything.

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #7

              @sierdzio said in Signal/slot and const parameters:

              I also recommend declaring signals as const

              Mixed feeling about this. Qt internally will const_cast the sender if the signal is const so it might be misleading to the user to assume the method is const. The moc of your signal will look something like this:

              // SIGNAL 0
              void DataCollrecor::newMessage(const MessageInfo & _t1)const
              {
                  void *_a[] = { Q_NULLPTR, const_cast<void*>(reinterpret_cast<const void*>(&_t1)) };
                  QMetaObject::activate(const_cast< DataCollrecor *>(this), &staticMetaObject, 0, _a);
              }
              

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              sierdzioS 1 Reply Last reply
              0
              • KroMignonK Offline
                KroMignonK Offline
                KroMignon
                wrote on last edited by
                #8

                Thanks @sierdzio and @VRonin for your inputs, just complete my questioning, what will happen if signal/slot parameter don't match with "const &".
                To continue with my basic example:

                • void newMessage(MessageInfo message);
                • void onNewMessage(const MessageInfo& message);

                or:

                • void newMessage(const MessageInfo& message);
                • void onNewMessage(MessageInfo message);

                Is there a risk to loose data or getting software crashes?

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                VRoninV 1 Reply Last reply
                0
                • KroMignonK KroMignon

                  Thanks @sierdzio and @VRonin for your inputs, just complete my questioning, what will happen if signal/slot parameter don't match with "const &".
                  To continue with my basic example:

                  • void newMessage(MessageInfo message);
                  • void onNewMessage(const MessageInfo& message);

                  or:

                  • void newMessage(const MessageInfo& message);
                  • void onNewMessage(MessageInfo message);

                  Is there a risk to loose data or getting software crashes?

                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #9

                  @KroMignon said in Signal/slot and const parameters:

                  what will happen if signal/slot parameter don't match with "const &".

                  You are calling a useless copy constructor

                  Is there a risk to loose data or getting software crashes?

                  No, just an efficiency issue

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  KroMignonK 1 Reply Last reply
                  5
                  • VRoninV VRonin

                    @KroMignon said in Signal/slot and const parameters:

                    what will happen if signal/slot parameter don't match with "const &".

                    You are calling a useless copy constructor

                    Is there a risk to loose data or getting software crashes?

                    No, just an efficiency issue

                    KroMignonK Offline
                    KroMignonK Offline
                    KroMignon
                    wrote on last edited by
                    #10

                    @VRonin and @sierdzio thanks a lot for your time.

                    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                    1 Reply Last reply
                    1
                    • VRoninV VRonin

                      @sierdzio said in Signal/slot and const parameters:

                      I also recommend declaring signals as const

                      Mixed feeling about this. Qt internally will const_cast the sender if the signal is const so it might be misleading to the user to assume the method is const. The moc of your signal will look something like this:

                      // SIGNAL 0
                      void DataCollrecor::newMessage(const MessageInfo & _t1)const
                      {
                          void *_a[] = { Q_NULLPTR, const_cast<void*>(reinterpret_cast<const void*>(&_t1)) };
                          QMetaObject::activate(const_cast< DataCollrecor *>(this), &staticMetaObject, 0, _a);
                      }
                      
                      sierdzioS Offline
                      sierdzioS Offline
                      sierdzio
                      Moderators
                      wrote on last edited by
                      #11

                      @VRonin said in Signal/slot and const parameters:

                      Mixed feeling about this. Qt internally will const_cast the sender if the signal is const

                      :O I had no idea.

                      (Z(:^

                      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