Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to pass variables as reference from C++ to QML
Forum Updated to NodeBB v4.3 + New Features

How to pass variables as reference from C++ to QML

Scheduled Pinned Locked Moved QML and Qt Quick
11 Posts 6 Posters 12.2k 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.
  • M Offline
    M Offline
    miroslav
    wrote on last edited by
    #2

    Make sure you have the Q_OBJECT macro in your header file (just because it is not in the listing you posted). For a slot, that should already do it, assuming you added objectA as a context property t to the QML context.

    Mirko Boehm | mirko@kde.org | KDE e.V.
    FSFE Fellow
    Qt Certified Specialist

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dmendizabal
      wrote on last edited by
      #3

      Yes Q_OBJECT and I'm exposing objectA as a context property.
      When I run the test I receive the following message:

      @Unknown method parameter type: int&
      @

      1 Reply Last reply
      0
      • M Offline
        M Offline
        miroslav
        wrote on last edited by
        #4

        Hm, not sure. It is a bit awkward to see non-const references there. Does it work with const int&? Not that that makes a whole lot of sense.
        References in slot parameters feel weird since it is unknown how many receivers a slot has, so how would you know which signal was calling you? And then there are queued connections. And whatnot. Looks strange.

        Mirko Boehm | mirko@kde.org | KDE e.V.
        FSFE Fellow
        Qt Certified Specialist

        1 Reply Last reply
        0
        • B Offline
          B Offline
          billouparis
          wrote on last edited by
          #5

          Don't you need the Q_INVOKABLE macro somehow?
          Bill

          1 Reply Last reply
          0
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #6

            in slots it's not necessary.

            (Z(:^

            1 Reply Last reply
            0
            • B Offline
              B Offline
              billouparis
              wrote on last edited by
              #7

              Thank you,
              Bill

              1 Reply Last reply
              0
              • D Offline
                D Offline
                dmendizabal
                wrote on last edited by
                #8

                @miroslav: Conceptually you are right. But in this particular case, the slot isn't used in a signal & slot context, but it is being called directly from QML side. Saying that, it shouldn't be a problem to provide arguments as reference.
                In any case, I also tried making the function public and using Q_INVOKABLE to be exposed to QML and the result is the same.

                At the end, there is always work arounds to solve problems. In this case, I had to create 2 functions to provide the necessary paramenters as return values. But it is kind of duplication of code and CPU usage.

                is there an official location to make suggestion or bug reports to the active developers?

                1 Reply Last reply
                0
                • sierdzioS Offline
                  sierdzioS Offline
                  sierdzio
                  Moderators
                  wrote on last edited by
                  #9

                  Yes, you can find it "here":https://bugreports.qt-project.org/secure/Dashboard.jspa.

                  (Z(:^

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    richardmg
                    wrote on last edited by
                    #10

                    You need to use QVariant when passing arguments to c++:

                    @
                    void func(const QVariant &arg1, const QVariant &arg2) {
                    int i = arg.toInt();
                    QString s = arg.toString();
                    }
                    @

                    1 Reply Last reply
                    1
                    • C Offline
                      C Offline
                      chrisadams
                      wrote on last edited by
                      #11

                      @richardmg: You don't need to use QVariant, no. When calling a function of a QObject from JavaScript, the QObject wrapper / interface class attempts to find the closest overload depending on the argument types.

                      It's true that some argument types are unknown to the QML typesystem, and for those wrapping in QVariants may be necessary (although they'll be opaque to JavaScript code) but for other types, it is not necessary.

                      Internally, basic types are indeed wrapped into QVariants when they're stored, but this should be transparent to the application programmer.

                      @dmendizabal: object introspection allows code to dynamically look up your objects' methods at runtime, and arbitrarily connect signals to your slots. Thus, if you have a Q_SLOT, it must abide by the rules for signal/slot connection, regardless of whether your code specifically makes use of it.

                      In most cases, for Qt built-in types, implicit sharing means that references are no cheaper than copies anyway.

                      Cheers,
                      Chris.

                      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