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. Can you discover Qt::ConnectionType after a connect(Qt::AutoConnection)?
Forum Updated to NodeBB v4.3 + New Features

Can you discover Qt::ConnectionType after a connect(Qt::AutoConnection)?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 122 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.
  • JonBJ Offline
    JonBJ Offline
    JonB
    wrote last edited by JonB
    #1

    After a (successful)

    QMetaObject::Connection connection = connect(..., Qt::AutoConnection);
    

    at runtime, can you discover which Qt::ConnectionType was chosen by Qt? I thought it would be a member of the returned QMetaObject::Connection but seemingly not? Qt does resolve/pick for Qt::AutoConnection during the connect() statement, doesn't it?

    I do not want to pass an explicit Qt::ConnectionType as I want the default behaviour on Qt::AutoConnection, e.g. if the objects are in different threads I need Qt to pick the queued type. But I wish to have the option to follow it with Q_ASSERT(connection.type == Qt::DirectConnection), as if it is not direct I need to reconsider the logic.

    Christian EhrlicherC 1 Reply Last reply
    0
    • JonBJ JonB

      After a (successful)

      QMetaObject::Connection connection = connect(..., Qt::AutoConnection);
      

      at runtime, can you discover which Qt::ConnectionType was chosen by Qt? I thought it would be a member of the returned QMetaObject::Connection but seemingly not? Qt does resolve/pick for Qt::AutoConnection during the connect() statement, doesn't it?

      I do not want to pass an explicit Qt::ConnectionType as I want the default behaviour on Qt::AutoConnection, e.g. if the objects are in different threads I need Qt to pick the queued type. But I wish to have the option to follow it with Q_ASSERT(connection.type == Qt::DirectConnection), as if it is not direct I need to reconsider the logic.

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote last edited by
      #2

      @JonB said in Can you discover Qt::ConnectionType after a connect(Qt::AutoConnection)?:

      can you discover which Qt::ConnectionType was chosen by Qt

      No since it's selected during the execution of the slot and therefore might change depending on where the two objects live.

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

      JonBJ 1 Reply Last reply
      3
      • Christian EhrlicherC Christian Ehrlicher

        @JonB said in Can you discover Qt::ConnectionType after a connect(Qt::AutoConnection)?:

        can you discover which Qt::ConnectionType was chosen by Qt

        No since it's selected during the execution of the slot and therefore might change depending on where the two objects live.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote last edited by JonB
        #3

        @Christian-Ehrlicher
        Ah. So, just as an example, you could have a

        connect(signalObject, ..., slotObject, ..., Qt::AutoConnection);
        

        where when the connect() statement is executed to make the connection the two objects live in the same thread but then you move one of them to another thread, and the connection would change from direct to queued effectively at the time the signal is emitted, right?

        OK. This means then that the overhead/implementation is a bit "heavier" at signal emit time than I had thought. For example, when docs etc. say "(with AutoConnection and single thread) an emit signal() gives a direct call to slot(s)", there is actually a runtime check in the signal function call each time it is emitted to determine whether signal object and slot object are in same thread?

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

          It's a simply QObject::thread() == receiver->thread(), yes. See https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/kernel/qobject.cpp#n4315

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

          S 1 Reply Last reply
          1
          • JonBJ JonB has marked this topic as solved
          • Christian EhrlicherC Christian Ehrlicher

            It's a simply QObject::thread() == receiver->thread(), yes. See https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/kernel/qobject.cpp#n4315

            S Offline
            S Offline
            SimonSchroeder
            wrote last edited by
            #5

            @Christian-Ehrlicher But isn't checking the thread at least slightly expensive? Or does the QObject just store an "integer"? Otherwise couldn't the proper connection type be automatically set when calling connect and moveToThread could do the heavy lifting just a single time to update the connection type of all connections (that have been set up with AutoConnection). We could at least save some conditionals (and quickly looking at the code some atomics).

            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