Setting object type property in QML from C++

Doug

(Editted to add more context)

I've started using QML and I'd like to set some sort of reference property on a QML type, linking two QML objects (ideally, without a parent/child relationship as I'd like to connect multiple QML objects).

For example, I have the following files.

qmldir:

A 1.0 A.qml

A.qml

import QtQuick 2.2

Rectangle {
    width: 100
    height: 100
    color: 'red'
    // Other stuff later
}

main.qml:

import QtQuick 2.2
import QtQuick.Window 2.1
import "qrc:/"

Rectangle {
    objectName: "Main window"
    visible: true
    width: 360
    height: 360

    MouseArea {
        anchors.fill: parent
        onClicked: {
            Qt.quit();
        }
    }

    Text {
        text: qsTr("Hello World")
        anchors.centerIn: parent
    }

    property A referencedObject;

    Rectangle {
        id: subView
        objectName: "subView"
    }
}

What I'd like to do with this is set the value of 'referencedObject' to an object created by C++ at runtime. However, the function for setting properties doesn't allow for QObject pointers or QQuickItem pointers, as QVariant objects can't be constructed that way.

Something like:

QQmlComponent aTemplate(&engine, QString("qrc:/A.qml"), &parentObject);
QQuickItem* aInstance = aTemplate.create();

aInstance->setParent(&parentObject);
mainView.setProperty("referencedObject",aInstance); // Won't work.

I'd like to keep the 'A' type object in QML because a) less boilerplate than C++ for this, and b) its meant to be a separate graphical object with life of its own, and QML works better for that use-case.

Thanks for any help posted.

Programmer

The following example shows how to set properties of objects of your custom type defined in QML from C++.

A.qml

import QtQuick 2.2
Rectangle {
    width: 0
    height: 0
    color:"red"
}

main.qml

import QtQuick 2.2  
Rectangle {
    visible: true
    width: 640
    height: 480

    property alias referencedObject:aProperty;

    A
    {
        id:aProperty
        objectName: "aPropertyObject"//Needed to access it from C++
    }
}

Now we have defined a qml type A. main.qml has a property of this Custom type. We need to change properties of this object from C++. Lets change the width,height and color.

main.cpp

#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQuickView>
#include <QQuickItem>
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QQmlApplicationEngine engine;
    QQmlComponent aTemplate(&engine, QUrl(("qrc:///A.qml")));
    QQuickItem* aInstance =qobject_cast<QQuickItem*>(aTemplate.create());
    aInstance->setWidth(100);
    aInstance->setHeight(100);
    aInstance->setProperty("color",QColor("green"));
    if(aInstance)
    {
        QQuickView *mainView = new QQuickView;
        mainView->setSource(QUrl(QStringLiteral("qrc:///main.qml")));
        mainView->show();
        QQuickItem * aPropertyObject = mainView->rootObject()->findChild<QQuickItem*>("aPropertyObject");
        if(aPropertyObject)
        {
           //Now you have pointers to both source and destination.
           //You can write a helper function which assigns the values
           //of source to the destination.
           //For the sake of demonstration, I am just setting some properties.       
           aPropertyObject->setWidth(aInstance->width());
           aPropertyObject->setHeight(aInstance->height());
           aPropertyObject->setProperty("color",aInstance->property("color"));
        }
    }
    return app.exec();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to bind a property to a singleton object property from QML

From Dev

QML object property changed from c++, but I can't see the result

From Dev

QML object property changed from c++, but I can't see the result

From Dev

Read property from QML singleton with C++

From Dev

Setting property value from other property value inside object

From Dev

QML not registering property change from C++ property

From Dev

call QML function from C++ with another QML object as parameter

From Dev

Type of objects passed from Qml to C++

From Dev

QML object var property is null QVariant(QJSValue, ) from cpp

From Dev

Confusion regarding the setting the property of a parent object from child object in javascript

From Dev

Ramda Js: Setting property on an object using a value from the same object

From Dev

Dynamically create C++ object from QML

From Dev

Changing QML Object value from C++

From Dev

Swift: check type of property from object (Reflection)

From Dev

How to find a property type from javascript object?

From Dev

Swift: check type of property from object (Reflection)

From Dev

QML object property memory management

From Dev

QML object property memory management

From Dev

Setting an object's properties from an NSDictionary with keys that are not identical to the property names

From Dev

Load property from QML-file in C++

From Dev

Qt / QML set property from c++ class for GridView

From Dev

Qt / QML set property from c++ class for GridView

From Dev

Load property from QML-file in C++

From Dev

Objective-C: Property not found on object of type

From Dev

Setting a Class Object as a property of another Class in Objective-C

From Dev

QML Access object property by property name string

From Java

Unable to access Swift 4 class from Objective-C: "Property not found on object of type"

From Dev

Setting data attributes to jQuery object from script type="text/template"

From Dev

Setting data attributes to jQuery object from script type="text/template"

Related Related

  1. 1

    How to bind a property to a singleton object property from QML

  2. 2

    QML object property changed from c++, but I can't see the result

  3. 3

    QML object property changed from c++, but I can't see the result

  4. 4

    Read property from QML singleton with C++

  5. 5

    Setting property value from other property value inside object

  6. 6

    QML not registering property change from C++ property

  7. 7

    call QML function from C++ with another QML object as parameter

  8. 8

    Type of objects passed from Qml to C++

  9. 9

    QML object var property is null QVariant(QJSValue, ) from cpp

  10. 10

    Confusion regarding the setting the property of a parent object from child object in javascript

  11. 11

    Ramda Js: Setting property on an object using a value from the same object

  12. 12

    Dynamically create C++ object from QML

  13. 13

    Changing QML Object value from C++

  14. 14

    Swift: check type of property from object (Reflection)

  15. 15

    How to find a property type from javascript object?

  16. 16

    Swift: check type of property from object (Reflection)

  17. 17

    QML object property memory management

  18. 18

    QML object property memory management

  19. 19

    Setting an object's properties from an NSDictionary with keys that are not identical to the property names

  20. 20

    Load property from QML-file in C++

  21. 21

    Qt / QML set property from c++ class for GridView

  22. 22

    Qt / QML set property from c++ class for GridView

  23. 23

    Load property from QML-file in C++

  24. 24

    Objective-C: Property not found on object of type

  25. 25

    Setting a Class Object as a property of another Class in Objective-C

  26. 26

    QML Access object property by property name string

  27. 27

    Unable to access Swift 4 class from Objective-C: "Property not found on object of type"

  28. 28

    Setting data attributes to jQuery object from script type="text/template"

  29. 29

    Setting data attributes to jQuery object from script type="text/template"

HotTag

Archive