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. Call invokeMethod passing argument by (no const) reference - Q_ARG(Type&, value)
QtWS25 Last Chance

Call invokeMethod passing argument by (no const) reference - Q_ARG(Type&, value)

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 364 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.
  • N Offline
    N Offline
    nico88desmo
    wrote on last edited by
    #1

    Dear all,

    just a simple question: suppose having a slot defined in this way:

    class Sample : public QObject {
       Q_OBJECT
       ...
    public slots:
       int populateBytes(QByteArray& bytes);
       ...
    }
    

    I'm calling that method using QMetaObject::invokeMethod() cause its object instance is living in another thread.
    I'm using a BlockingQueuedConnection, cause I need to wait method results; the calling function is similar to this one:

    void testFunction() {
       int result;
       QByteArray testBytes;
    
       QMetaObject::invokeMethod(
          &objInstance,
          "populateBytes",
          Qt::BlockingQueuedConnection,
          Q_RETURN_ARG(result, int),
          Q_ARG(QByteArray&, testBytes)  // <--- is it correct???
       );
       return result;
    }
    

    From Q_ARG documentation, I expect that isn't possible passing a parameter using a no const value. Instead it seems working.

    Going a bit deeper, I see that Q_ARG is define as follow:
    #define Q_ARG(type, data) QArgument<type >(#type, data)

    where:

    template <class T>
    class QArgument: public QGenericArgument
    {
    public:
        inline QArgument(const char *aName, const T &aData)
            : QGenericArgument(aName, static_cast<const void *>(&aData)) // <--- const void* !!!
            {}
    };
    template <class T>
    class QArgument<T &>: public QGenericArgument
    {
    public:
        inline QArgument(const char *aName, T &aData)
            : QGenericArgument(aName, static_cast<const void *>(&aData)) // <--- const void* !!!
            {}
    };
    

    So, in both solution, aData will be use as a const void* parameter.

    Am I "forcing" some const value to be no const?
    In another way, is it safe passing a variable by reference, instead of const reference in a QMetaObject::invokeMethod(..., Q_ARG(Type&, value),..) call?

    I'm using Qt 6.2.7

    Thanks all.

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

      You're aware that QMetaObject::invokeMethod() does not block so you can't return anything if it is async. Again: a slot does not return anything - your design is broken, fix it.

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

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

        A slot can never have a (non-const) reference since a slot might be executed asynchronously.
        A slot is doing something but never bringing something back so your populateBytes() is not a slot at all.

        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
        0
        • N Offline
          N Offline
          nico88desmo
          wrote on last edited by nico88desmo
          #3

          Generally I'm agree to you @Christian-Ehrlicher.

          But, in case I'm using that slot only via Qt::DirectConnection or Qt::BlockingQueueConnection, is not possible using a (non-const) reference?

          From some tests, I see that testBytes is correctly populated using Qt::BlockingQueueConnection, but I'm not sure if this is an exceptional case or not.

          1 Reply Last reply
          0
          • N Offline
            N Offline
            nico88desmo
            wrote on last edited by
            #4

            Or, maybe, a better solution is passing a QByteArray*: in this case, it's my responsibity to assure that allocate space is keeping available in async call..

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

              You're aware that QMetaObject::invokeMethod() does not block so you can't return anything if it is async. Again: a slot does not return anything - your design is broken, fix it.

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

              N 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                You're aware that QMetaObject::invokeMethod() does not block so you can't return anything if it is async. Again: a slot does not return anything - your design is broken, fix it.

                N Offline
                N Offline
                nico88desmo
                wrote on last edited by
                #6

                @Christian-Ehrlicher Ok, i'm going to use a signal/slot like solution.

                It's both more clean and safety.

                1 Reply Last reply
                0
                • N nico88desmo has marked this topic as solved on

                • Login

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