Read property from QML singleton with C++

Tobias Sch.

Is it possible to access/read the properties of a QML singleton inside your C++ code?

For example if my QML singleton looks like this:

pragma Singleton
import QtQuick 2.5

QtObject {
  property int myProperty: 5
}

How can I access myProperty from C++ code. I need this as I do not want to have my "magic" numbers both in QML and C++ and it is only very rarely required in C++.

For normal QQuickItem's it was always easy. Just get access to the QuickItem (by dynamic creating it or with findChild()) and than call quickItem->property("myProperty").toInt() But with the singleton I can't see how to get access to it.

Benp44

Although not directly, one way to access a QML singleton is via a function in a non-singleton QML object, that you can access in the usual way:

Constants.qml

pragma Singleton

import QtQuick 2.5

QtObject {
    objectName: "Constants"
    property double phi: 1.6180339887498948482
}

main.qml (e.g.)

import QtQuick 2.5
import "."

function getPhi()
{
    return Constants.phi;
}

C++

//...
// Create the engine and load QML
//...

QObject* rootObject = engine->rootObjects().constFirst();

QVariant phi;
QMetaObject::invokeMethod(rootObject, "getPhi", Q_RETURN_ARG(QVariant, phi));
qDebug() << phi.toFloat();

Don't forget you'll need a qmldir file to access the singleton in QML:

qmldir

singleton Constants Constants.qml

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Change property of the QML singleton with C++

From Dev

Change property of the QML singleton with C++

From Dev

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

From Dev

QML not registering property change from C++ property

From Dev

Setting object type property in QML from C++

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

How to connect signals from singleton object defined in C++ code to QML component?

From Dev

QML: Cannot read property 'xxx' of undefined

From Dev

"Invalid property assignment:.. read-only property" in Qml

From Dev

Read property from c++ dll into c# class?

From Dev

Setter of a singleton from an custome class in .Net C# not triggered if only one property changed

From Dev

QML - connect a signal from c++ to qml

From Dev

Access property set with setProperty 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

QML: How to correctly pass property to PluginParameter value from C++ code?

From Dev

qml c++ property bindings with multiple threads

From Dev

read from text file in qml qt/quick

From Dev

TypeError: Cannot read property 'length' of undefined in QML array

From Dev

Qt QML Singleton Intellisense

From Dev

Cannot read property from undefined

From Dev

QObject from C++ to QML to QML to C++ (in a list)

From Dev

Maven, using property from property file to read another property file

From Dev

How to get updated property in singleton from a component in Blazor?

From Dev

QT/QML c++ Program crash on access a QList from QML

From Dev

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

Related Related

  1. 1

    Change property of the QML singleton with C++

  2. 2

    Change property of the QML singleton with C++

  3. 3

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

  4. 4

    QML not registering property change from C++ property

  5. 5

    Setting object type property in QML from C++

  6. 6

    Load property from QML-file in C++

  7. 7

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

  8. 8

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

  9. 9

    Load property from QML-file in C++

  10. 10

    How to connect signals from singleton object defined in C++ code to QML component?

  11. 11

    QML: Cannot read property 'xxx' of undefined

  12. 12

    "Invalid property assignment:.. read-only property" in Qml

  13. 13

    Read property from c++ dll into c# class?

  14. 14

    Setter of a singleton from an custome class in .Net C# not triggered if only one property changed

  15. 15

    QML - connect a signal from c++ to qml

  16. 16

    Access property set with setProperty from QML

  17. 17

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

  18. 18

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

  19. 19

    QML: How to correctly pass property to PluginParameter value from C++ code?

  20. 20

    qml c++ property bindings with multiple threads

  21. 21

    read from text file in qml qt/quick

  22. 22

    TypeError: Cannot read property 'length' of undefined in QML array

  23. 23

    Qt QML Singleton Intellisense

  24. 24

    Cannot read property from undefined

  25. 25

    QObject from C++ to QML to QML to C++ (in a list)

  26. 26

    Maven, using property from property file to read another property file

  27. 27

    How to get updated property in singleton from a component in Blazor?

  28. 28

    QT/QML c++ Program crash on access a QList from QML

  29. 29

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

HotTag

Archive