Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Sending parameters from C++
Qt 6.12 LTS is out! See what's new in the release blog

Sending parameters from C++

Scheduled Pinned Locked Moved QML and Qt Quick
11 Posts 4 Posters 9.4k 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.
  • D Offline
    D Offline
    DenisKormalev
    wrote on last edited by
    #2

    you can use something like
    @
    view->rootContext()->setContextProperty("listModel", model);
    @

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #3

      [quote author="Denis Kormalev" date="1291291116"]you can use something like
      @
      view->rootContext()->setContextProperty("listModel", model);
      @
      [/quote]

      what I'm looking for is something like this. I have this JS function on the QML/JS side:

      @WorkerScript.onMessage = function(msg) {
      if (msg.action == 'appendCurrentTime') {
      var data = {'time': new Date().toTimeString()};
      msg.model.append(data);
      msg.model.sync(); // updates the changes to the list
      }
      }
      @

      from QML/JS side I can call this function like this:
      @var msg = {'action': 'appendCurrentTime', 'model': listModel};
      worker.sendMessage(msg);
      @

      but I want to call this function from C++ and send this parameter:
      @var msg = {'action': 'appendCurrentTime', 'model': listModel};@

      Is it possible? or I need to update the entire ListModel from C+?

      1 Reply Last reply
      0
      • X Offline
        X Offline
        xsacha
        wrote on last edited by
        #4

        Not sure if this is what you mean, but I am using this:

        In C++:
        appdata.h
        @class ApplicationData : public QObject
        {
        Q_OBJECT
        public:
        Q_INVOKABLE QDateTime getCurrentDateTime() const {
        return QDateTime::currentDateTime();
        }
        };
        @
        main.cpp
        @#include "appdata.h"
        ...
        viewer.rootContext()->setContextProperty("applicationData", &data);@

        in QML:
        @logDir.appendContent(Qt.formatDateTime(applicationData.getCurrentDateTime(), "hh:mm")@

        To refresh the content, you can use the Timer as per that JS example you have. The call will always retrieve the latest value.

        • Sacha
        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #5

          oh ok. I thing I gave a bad example.
          In QML/JS you call a function using this parameter:

          @var msg = {'propertyName1': 'propertyVal1', 'propertyName2': propertyVal2};
          someFunction(msg);
          @

          I want to call the JS function: someFunction from C++ with the same parameter, but I don't know how to write that parameter in C++ :
          @var msg = {'propertyName1': 'propertyVal1', 'propertyName2': propertyVal2};@

          1 Reply Last reply
          0
          • B Offline
            B Offline
            blam
            wrote on last edited by
            #6

            The JavaScript function isn't available to C++. So the easiest way is do what you want is to create a method in your QML component that passes the method call onto the JS function. Then, you can call the method in the QML component from C++.

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #7

              [quote author="blam" date="1291335496"]The JavaScript function isn't available to C++. So the easiest way is do what you want is to create a method in your QML component that passes the method call onto the JS function. Then, you can call the method in the QML component from C++.

              [/quote]

              Thanks, I knew that, but can I send that variable from C++ to QML? or I need to create in JS a function with simple parameters(strings and integers for example) and than on that function create the msg variable and call the other JS function .

              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #8

                The idea is that I know how to make this work, what I'm looking for is the best way to implement this. That's why I was curious if I can pass "complex" variables from C++ to qml .

                1 Reply Last reply
                0
                • X Offline
                  X Offline
                  xsacha
                  wrote on last edited by
                  #9

                  We gave examples of sending a variable from C++ to QML using setContextProperty(..).

                  Do you want to see a more complete example with additional variables?

                  You can send a whole class from C++ to QML as a Type.

                  The backend is 100% Qt, so this is how all those Types are done in the first place.

                  • Sacha
                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    blam
                    wrote on last edited by
                    #10

                    Ah, I see, you mean passing something like a js object from C++ to QML?

                    You can pass a QVariantMap value. For example this C++:

                    @
                    QVariantMap map;
                    map.insert("aaa", 111);
                    map.insert("bbb", 222);
                    QMetaObject::invokeMethod(myQmlObject, "someFunction", Q_ARG(QVariant, QVariant::fromValue(map)));
                    @

                    can call this QML:

                    @
                    Item {
                    function someFunction(value) {
                    console.log(value.aaa, value.bbb) // prints "111, 222"
                    }
                    }
                    @

                    1 Reply Last reply
                    0
                    • ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #11

                      thanks blam. I will try your example. looks similar to what I'm looking for.

                      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
                      • Unsolved