[Solved] QtQuick Application recommended "To-Do"s

  • hello,

    can you give me some exercises on Qml and C++ binding or some examples, if you have some.
    i already did some binding with slots, but i want to do more than (or then, excuse my grammar) printing some data in a qml window.

    thank you!

  • Hey,

    your Viewer has got a rootContext() and a rootObject(). Lets just take the rootObject:
    @QQuickView *view = new QQuickView();
    QObject object = (QObject)view->rootObject();@

    The rootObject is your top-level object in qml and has got all other components as children. If you are searching for a children you should give it an objectName.
    @Rectangle {
    objectName: "myRectangle"
    // some code

    Now we search for our Rectangle using the function findChild().
    @QObject myObject = object->findChild<QObject>("myRectangle");
    // We got it :)

    Lets go ahead and set a property:
    @object->setProperty("propertyName", QVariant("someValue"));@

    It’s also easy to get a property:
    @QVariant ourProperty = object->property("propertyName");@

    You could use QMetaObject::invokeMethod to invoke a QML method.
    For example:
    @QQuickView *view = new QQuickView();
    QObject object = (QObject)view->rootObject();
    QMetaObject::invokeMethod(object, "nameOfYourQmlFunction");@

    If you want to pass parameters:
    @QMetaObject::invokeMethod(object, "nameOfYourQmlFunction", Q_ARG(QVariant, "valueToSend"))@

    You can replace the String with booleans or integers, QML transforms them to it’s required format. If you want to have some values returned you’ll need a Q_RETURN_ARG instead of the Q_ARG.
    Function in the Documentation:
    @bool QMetaObject::invokeMethod(QObject * obj, const char * member, Qt::ConnectionType type, QGenericReturnArgument ret, QGenericArgument val0 = QGenericArgument( 0 ), QGenericArgument val1 = QGenericArgument(), QGenericArgument val2 = QGenericArgument(), QGenericArgument val3 = QGenericArgument(), QGenericArgument val4 = QGenericArgument(), QGenericArgument val5 = QGenericArgument(), QGenericArgument val6 = QGenericArgument(), QGenericArgument val7 = QGenericArgument(), QGenericArgument val8 = QGenericArgument(), QGenericArgument val9 = QGenericArgument())[static]@

    For further informations i would recommend you to read:
    "Using QML Bindings in C++ Applications":http://qt-project.org/doc/qt-4.8/qtbinding.html
    "Interacting with QML Objects from C++":http://qt-project.org/doc/qt-5.0/qtqml/qtqml-cppintegration-interactqmlfromcpp.html
    "Calling Functions":http://qt-project.org/doc/qt-4.8/qtbinding.html#calling-functions
    "Receiving Signals":http://qt-project.org/doc/qt-4.8/qtbinding.html#receiving-signals
    "Supportet Datatypes":http://qt-project.org/doc/qt-4.8/qtbinding.html#supported-data-types

    Beside of that you can also provide a custom Cpp model to QML.

  • thank you.

    i find your post more helpful than what i could find on qt-project.org.

    off topic: i'm new on qt and forums and whatnot. if you can close this topic as solved, that would be great. :D

  • You're welcome, glad i could help you.
    It is not possible to close a thread, but you could edit your mainpost and add a [Solved] in front of the threadname.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.