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. Qt5 & invokeMethod
QtWS25 Last Chance

Qt5 & invokeMethod

Scheduled Pinned Locked Moved Solved General and Desktop
qt5signal & slotinvokemethod
4 Posts 3 Posters 7.0k Views
  • 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.
  • J.HilkJ Offline
    J.HilkJ Offline
    J.Hilk
    Moderators
    wrote on last edited by
    #1

    Hello everyone,

    I have a - hopefully - simple question:

    Is there a Qt5 Syntax-conform way to call QMetaObject::invokeMethod ?

    For example I use something like this to call a function in the next event loop cycle:

    QMetaObject::invokeMethod(this, "myFunction" ,Qt::QueuedConnection);
    

    But it is highly inconvenient.

    • The function call is not listed wenn you search for it!
    • Because "myFunction" is essentially a string, its not effected by simple refactoring e.g renaming.
    • And of course no compile-time validation check

    I know I could potentially use a QTimer:

    QTimer::singleShot(0,this, &myClass::myFunction);
    

    But that feels like circumventing the problem, also I'm nut sure if those 2 would always behave in the same way!


    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


    Q: What's that?
    A: It's blue light.
    Q: What does it do?
    A: It turns blue.

    raven-worxR 1 Reply Last reply
    0
    • J.HilkJ J.Hilk

      Hello everyone,

      I have a - hopefully - simple question:

      Is there a Qt5 Syntax-conform way to call QMetaObject::invokeMethod ?

      For example I use something like this to call a function in the next event loop cycle:

      QMetaObject::invokeMethod(this, "myFunction" ,Qt::QueuedConnection);
      

      But it is highly inconvenient.

      • The function call is not listed wenn you search for it!
      • Because "myFunction" is essentially a string, its not effected by simple refactoring e.g renaming.
      • And of course no compile-time validation check

      I know I could potentially use a QTimer:

      QTimer::singleShot(0,this, &myClass::myFunction);
      

      But that feels like circumventing the problem, also I'm nut sure if those 2 would always behave in the same way!

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @J.Hilk
      the 2 methods behave the same.
      QMetaObject::invokeMethod() only accepts the method as string because it is using the object's metaObject, which also only stores the methods as string.
      You can check the return value of invokeMethod() and add an assertion.

      bool check = QMetaObject::invokeMethod(...);
      Q_ASSERT( check );
      

      Just make sure not to surround the invokeMethod() call into Q_ASSERT otherwise they wont be executed in release mode.
      But you could add your custom assertion macro for this as well:

      #ifdef QT_DEBUG
      #define MY_ASSERT(CODE) Q_ASSERT(CODE);
      #else
      #define MY_ASSERT(CODE) CODE
      #endif
      
      MY_ASSERT( QMetaObject::invokeMethod(...) );
      

      or alternatively for both debug AND release (untested though - not sure if this compiles):

      #define MY_ASSERT(CODE) (CODE == false ? (qFatal() << #CODE << wasn't successful;) : (void;) )
      
      MY_ASSERT( QMetaObject::invokeMethod(...) );
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

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

        In Qt 5.10 (currently in alpha) there's gonna be an overload taking pointer to function, so you'll be able to do:

        QMetaObject::invokeMethod(this, &myClass::myFunction, Qt::QueuedConnection);
        

        For now I'd use the timer. The 0 timeout special case is documented so there's nothing wrong in using it.

        1 Reply Last reply
        4
        • J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          Thanks @raven-worx and @Chris-Kawa for the quick answers.

          @raven-worx said in Qt5 & invokeMethod:

          the 2 methods behave the same.

          I know that they seem to behave in the same way.
          But creating an timer object, running & processing the timer/timeout and deleteing the object afterwards, feels like it should be more work than queing a function call via Q_Object-Macro-Magic. However the compiler may break it down to the same thing, after all.

          @raven-worx said in Qt5 & invokeMethod:

          You can check the return value of invokeMethod() and add an assertion.

          That is actually a good idea, that way one would always get an immediate notification when the call becomes invalid due to changes made.
          Still inconvenient, but much less so.

          @Chris-Kawa said in Qt5 & invokeMethod:

          In Qt 5.10 (currently in alpha) there's gonna be an overload taking pointer to function, so you'll be able to do:

          YES, thanks for the info, I was not aware of that change.

          Now I'm hyped for Qt 5.10 more so than previously!

          With that said, I would consider my question answered.
          Thanks everyone.


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          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