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. Q_INVOKABLE cpp method and passing attributes by reference from qml

Q_INVOKABLE cpp method and passing attributes by reference from qml

Scheduled Pinned Locked Moved Unsolved General and Desktop
qmlcppmethodsexposing qml
5 Posts 3 Posters 3.8k 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.
  • A Offline
    A Offline
    ars1614
    wrote on last edited by ars1614
    #1

    Hi!
    I would like to work with some parameters in qml and modify them in cpp by function.
    The thing is that, I can accept them into an array of QObjects but I would like to pass them as parameters by reference.
    For now, the function returns a boolean and accepts a string and a list of strings.
    This would be the function: bool doSomething(QString strParameterOne, QSharedPointer<QVariantList> parameterTwo)
    Qml says: Error: Unknown method parameter type: QSharedPointer<QVariantList>
    I would like to be able to modify the parameters in cpp and see the changes in qml.

    Is this possible? Or the only way is to accept them in the return of the method?

    kshegunovK p3c0P 2 Replies Last reply
    0
    • A ars1614

      Hi!
      I would like to work with some parameters in qml and modify them in cpp by function.
      The thing is that, I can accept them into an array of QObjects but I would like to pass them as parameters by reference.
      For now, the function returns a boolean and accepts a string and a list of strings.
      This would be the function: bool doSomething(QString strParameterOne, QSharedPointer<QVariantList> parameterTwo)
      Qml says: Error: Unknown method parameter type: QSharedPointer<QVariantList>
      I would like to be able to modify the parameters in cpp and see the changes in qml.

      Is this possible? Or the only way is to accept them in the return of the method?

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      @ars1614 said:

      Is this possible? Or the only way is to accept them in the return of the method?

      I don't develop in QML, but I really, really, really doubt you can pass parameters by reference between the two. As far as I know QML has some internal threading and passing by reference would ruin the Qt developers' day (i.e. it basically would be a ticking race-condition bomb).

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • A ars1614

        Hi!
        I would like to work with some parameters in qml and modify them in cpp by function.
        The thing is that, I can accept them into an array of QObjects but I would like to pass them as parameters by reference.
        For now, the function returns a boolean and accepts a string and a list of strings.
        This would be the function: bool doSomething(QString strParameterOne, QSharedPointer<QVariantList> parameterTwo)
        Qml says: Error: Unknown method parameter type: QSharedPointer<QVariantList>
        I would like to be able to modify the parameters in cpp and see the changes in qml.

        Is this possible? Or the only way is to accept them in the return of the method?

        p3c0P Offline
        p3c0P Offline
        p3c0
        Moderators
        wrote on last edited by
        #3

        @ars1614 To add to @kshegunov's answer check this: Using QVariantMap to Emulate Passing by Reference and Pointer in QML.

        157

        A 1 Reply Last reply
        3
        • p3c0P p3c0

          @ars1614 To add to @kshegunov's answer check this: Using QVariantMap to Emulate Passing by Reference and Pointer in QML.

          A Offline
          A Offline
          ars1614
          wrote on last edited by
          #4

          @p3c0 This is the way to 'emulate' a JSObject isn't it?
          Another question that has come to me since your answer is If I have a JSObject like the one is written in the blog post and If I accept a QVariantMap in cpp, will it be automatically converted?

          p3c0P 1 Reply Last reply
          0
          • A ars1614

            @p3c0 This is the way to 'emulate' a JSObject isn't it?
            Another question that has come to me since your answer is If I have a JSObject like the one is written in the blog post and If I accept a QVariantMap in cpp, will it be automatically converted?

            p3c0P Offline
            p3c0P Offline
            p3c0
            Moderators
            wrote on last edited by
            #5

            @ars1614 Yes they are converted automatically.
            http://doc.qt.io/qt-5/qtqml-cppintegration-data.html#qvariantlist-and-qvariantmap-to-javascript-array-and-object

            157

            1 Reply Last reply
            2

            • Login

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