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. Passing references in signals and slots
Forum Updated to NodeBB v4.3 + New Features

Passing references in signals and slots

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 108 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.
  • P Offline
    P Offline
    Pippin
    wrote last edited by
    #1

    Hello,

    I'm sorry if the answer is available somewhere in the documentation, I'm just not sure where to look.

    If I pass a custom object (it may not be necessarily a QObject) by reference in signals and slots, will it be truly passed by reference, or will it get copied anyway?

    Is the answer to the above question likely to change in the future versions of Qt?

    Thank you.

    JonBJ 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote last edited by
      #2

      You can only pass const references. And they will not be copied except the connection is a queued connection or the slot does not take const references but values.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • P Pippin

        Hello,

        I'm sorry if the answer is available somewhere in the documentation, I'm just not sure where to look.

        If I pass a custom object (it may not be necessarily a QObject) by reference in signals and slots, will it be truly passed by reference, or will it get copied anyway?

        Is the answer to the above question likely to change in the future versions of Qt?

        Thank you.

        JonBJ Online
        JonBJ Online
        JonB
        wrote last edited by
        #3

        @Pippin
        As @Christian-Ehrlicher has written. But be aware that if your object is QObject-derived it cannot be copied anyway, so at least in a queued connection/cross-thread you presumably cannot do something which would require copying.

        Normally objects, including QObjects, are/should be passed as a pointer. I think (just about) every Qt function which accepts a QObject does so as a QObject * not a QObject &. Of course you have to be careful about accessing it certainly if cross-thread, e.g. mutexs and UI objects, like QWidgets, must not be accessed from a secondary thread.

        1 Reply Last reply
        1
        • P Offline
          P Offline
          Pippin
          wrote last edited by
          #4

          Thank you @Christian-Ehrlicher @JonB

          1 Reply Last reply
          0
          • P Pippin has marked this topic as solved
          • P Offline
            P Offline
            Pippin
            wrote last edited by
            #5

            If the object itself isn't const, but I pass it as a const reference through signals and slots, I guess that still works? As long as the signals and slots are declared with const&

            S 1 Reply Last reply
            0
            • P Pippin

              If the object itself isn't const, but I pass it as a const reference through signals and slots, I guess that still works? As long as the signals and slots are declared with const&

              S Offline
              S Offline
              SimonSchroeder
              wrote last edited by
              #6

              @Pippin said in Passing references in signals and slots:

              If the object itself isn't const, but I pass it as a const reference through signals and slots, I guess that still works? As long as the signals and slots are declared with const&

              Yes, it is sufficient if it is declared const& in the signals and slots.

              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