Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Sending parameters from C++



  • Hi everyone. So the short story. I'm looking at this example "Threaded ListModel Example":http://doc.qt.nokia.com/4.7/declarative-threading-threadedlistmodel.html and I want to have something similar . I mean I want to update the ListModel elements from C++.
    How can I send this parameter from C++?

    @var msg = {'action': 'appendCurrentTime', 'model': listModel};@

    Thanks



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



  • [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+?



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



  • 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};@



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



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



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



  • 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"
    }
    }
    @



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


Log in to reply