haggai_e haggai_e - 3 months ago 16
C++ Question

Accessing QML's LayoutMirroring attached property from C++

The

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
LayoutMirroring
values from C++ code, extending a QML application. I understand that the
qmlAttachedPropertiesObject
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?

Answer

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;
view.setSource(QUrl(QStringLiteral("qrc:/main.qml")));
view.show();

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

Console output:

=> QVariant(bool, true)