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. What is signficance of passing references in signals
Qt 6.11 is out! See what's new in the release blog

What is signficance of passing references in signals

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 6 Posters 13.1k Views 3 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.
  • Q Offline
    Q Offline
    Qt Enthusiast
    wrote on last edited by
    #1

    In my most of legacy code . I am seeing passing parameters to signals as references Please find the example below

    QObject::connect(sourceView, SIGNAL(signal(const QString&, const QString&, int)), d_destView, SLOT(slot(onst QString&, const QString&, int)));

    what is the significance of the same

    beeckscheB 1 Reply Last reply
    1
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      On paper references are faster as you don't have to copy the entire object but just copy its address (so 32 or 64 bits). In your example however, as QString is implicitly shared even passing by value would not trigger a full copy so in that snippet there is little to no advantage. In general, if the arguments don't need to be modified inside the method const references are often used for all but the basic types (int double, etc. as they are already 32 or 64 bits)

      "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
      1
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by Chris Kawa
        #3

        @VRonin said:

        little to no advantage

        I agree with the "little" part, but there is always an advantage. References are really just pointers. In case of implicitly shared object you need a pointer copy and refcount increase. Const references are just a pointer copy or not even that if the compiler optimizes it and the address is already in some cpu register. You also need to remember that implicit sharing involves atomic counters, which prevents some micro-optimizations the compiler can do.

        1 Reply Last reply
        2
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          To add to this, the actual connect statement should not include reference symbols if you use SIGNAL() and SLOT() as it will be slower, see http://doc.qt.io/qt-4.8/signalsandslots.html#advanced-signals-and-slots-usage

          "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

          Q 1 Reply Last reply
          0
          • Q Qt Enthusiast

            In my most of legacy code . I am seeing passing parameters to signals as references Please find the example below

            QObject::connect(sourceView, SIGNAL(signal(const QString&, const QString&, int)), d_destView, SLOT(slot(onst QString&, const QString&, int)));

            what is the significance of the same

            beeckscheB Offline
            beeckscheB Offline
            beecksche
            wrote on last edited by
            #5

            Hi @Qt-Enthusiast ,
            i'm not quite sure that i understand your question, but i found this about const references in signal and slots:

            Qt uses normalized signatures to decide whether two given signals and slots are compatible. Normalization reduces whitespace to a minimum, moves 'const' to the front where appropriate, removes 'const' from value types and replaces const references with values.

            Read it here.

            1 Reply Last reply
            0
            • VRoninV VRonin

              To add to this, the actual connect statement should not include reference symbols if you use SIGNAL() and SLOT() as it will be slower, see http://doc.qt.io/qt-4.8/signalsandslots.html#advanced-signals-and-slots-usage

              Q Offline
              Q Offline
              Qt Enthusiast
              wrote on last edited by
              #6

              @VRonin A

              as I understand it will be slower to pass references in Signals and Slorts . Let me know if I am correct in my understanding

              jsulmJ 1 Reply Last reply
              0
              • Q Qt Enthusiast

                @VRonin A

                as I understand it will be slower to pass references in Signals and Slorts . Let me know if I am correct in my understanding

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Qt-Enthusiast I think QObject::connect can be slower, but not calling the slots after the connection was established. As long as you do not do many connects in a loop there is no need to care about performance here.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • Chris KawaC Offline
                  Chris KawaC Offline
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on last edited by Chris Kawa
                  #8

                  When using the SIGNAL/SLOT macros the signatures are looked up/compared as strings at runtime. To do that const and & are removed from the string, so it's marginally faster to not include them in the first place.

                  What you really really should do is switch to the new connect syntax that uses function pointers:

                  QObject::connect(sourceView, &SourceViewClass::signal, d_destView, &DestViewClass::slot);
                  

                  It's easier, doesn't require to remember parameters and their types, does type checking at compile time instead of silently failing at runtime and is a lot faster, as there are no strings involved at all and matching is done at compile time.

                  1 Reply Last reply
                  1
                  • Q Offline
                    Q Offline
                    Qt Enthusiast
                    wrote on last edited by
                    #9

                    I am using qt.4.3.3 and if we can do that

                    1 Reply Last reply
                    0
                    • Chris KawaC Offline
                      Chris KawaC Offline
                      Chris Kawa
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Wow! 4.3.3 was released in like... 2007? From a security, bugfixes and support for current platforms standpoint it's ancient. Have you considered an update? At least to the top of the 4.x branch (which is pretty old by now too)?

                      1 Reply Last reply
                      1
                      • Q Offline
                        Q Offline
                        Qt Enthusiast
                        wrote on last edited by
                        #11

                        Considered but Need approval from my seniors

                        A 1 Reply Last reply
                        0
                        • Q Qt Enthusiast

                          Considered but Need approval from my seniors

                          A Offline
                          A Offline
                          Asperamanca
                          wrote on last edited by
                          #12

                          @Qt-Enthusiast said:

                          Considered but Need approval from my seniors

                          I feel your pain. Good luck on that!

                          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