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. const QObject* overload for QMetaObject::invokeMethod
Qt 6.11 is out! See what's new in the release blog

const QObject* overload for QMetaObject::invokeMethod

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.0k Views 1 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.
  • L Offline
    L Offline
    Loic B.
    wrote on last edited by
    #1

    Hello,

    I want to call QMetaObject::invokeMethod on a const QObject* but I see no overload accepting it as const. Why is there no overload ?

    Is it safe to const_cast<QObject *> to call QMetaObject::invokeMethod ? Is this will invoke UB ?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      How come you get a const QObject pointer to call invokeMethod with ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      L 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        How come you get a const QObject pointer to call invokeMethod with ?

        L Offline
        L Offline
        Loic B.
        wrote on last edited by Loic B.
        #3

        @SGaist i want to ensure that some member function are called in the thread of the object to avoid data races mostly. I decided to implement a function accepting a function object containing thé code to execute. If the current thread is the object one, il directly invoke the function object with std::invoke. If not, i call the function object on the object context via QMetaObject::invokeMethod.

        Some of the function i want to be called on the same thread are getters that I put const by default.

        Christian EhrlicherC 1 Reply Last reply
        0
        • L Loic B.

          @SGaist i want to ensure that some member function are called in the thread of the object to avoid data races mostly. I decided to implement a function accepting a function object containing thé code to execute. If the current thread is the object one, il directly invoke the function object with std::invoke. If not, i call the function object on the object context via QMetaObject::invokeMethod.

          Some of the function i want to be called on the same thread are getters that I put const by default.

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

          @Loic-B said in const QObject* overload for QMetaObject::invokeMethod:

          Some of the function i want to be called on the same thread are getters that I put const by default.

          Then it's not a slot and there is no need that it's called via QMetaMethod.

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

          L 1 Reply Last reply
          1
          • Christian EhrlicherC Christian Ehrlicher

            @Loic-B said in const QObject* overload for QMetaObject::invokeMethod:

            Some of the function i want to be called on the same thread are getters that I put const by default.

            Then it's not a slot and there is no need that it's called via QMetaMethod.

            L Offline
            L Offline
            Loic B.
            wrote on last edited by
            #5

            @Christian-Ehrlicher so how can i ensure that without using the QMetaObject::invokeMethod ? By using mutexes ?

            Christian EhrlicherC 1 Reply Last reply
            0
            • L Loic B.

              @Christian-Ehrlicher so how can i ensure that without using the QMetaObject::invokeMethod ? By using mutexes ?

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

              @Loic-B said in const QObject* overload for QMetaObject::invokeMethod:

              By using mutexes ?

              If you access an object from different threads then you have to apply correct locking. Nothing Qt specific but Qt has a nice page about thread synchronization.

              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
              1

              • Login

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