haggai_e haggai_e - 1 month ago 6x
C++ Question

Accessing QML's LayoutMirroring attached property from C++


QML attached property allows switching the directionality of an item from left-to-right to right-to-left, or vice versa. I would like to access an item's current
values from C++ code, extending a QML application. I understand that the
function can be used to access attached properties from C++, as explained in the documentation, but I don't know where I can find the class that represent this specific property in C++.

Is it possible to do that?


It's possible to read and write attached properties easily with QQmlProperty class since Qt5.

Simple main.qml file:

import QtQuick 2.0

Rectangle {
    LayoutMirroring.enabled: true
    LayoutMirroring.childrenInherit: true

Simple C++ code to read above LayoutMirroring.enabled attached property:

QQuickView view;

QQuickItem* rootItem = view.rootObject();
qDebug() << QQmlProperty::read(rootItem, "LayoutMirroring.enabled", qmlContext(rootItem));

Console output:

=> QVariant(bool, true)