In QML is there a way of getting the top-level Window of any visual object? Preferably without recursing up through the visual parent hierarchy.
I'm trying to find the geometry of the top-level window, so descendent objects can detect if their bounds have crossed the window's.
There are Window properties attached to all Items. Which ones depend on Qt version. E.g. Window.width is the current top level window/view width.
You can get a particular Item's Window with myItem.Window;
With Qt 5.7+ you even get access to all the Window properties via Window.window.
See docs: http://doc.qt.io/qt-5/qml-qtquick-window-window.html#attached-properties
I guess the answer at the moment is "No". This looks like a feature suggestion that can be sent to the QML team.
I ended up exporting my own C++ class to QML.
ItemWithWindow.h:
#include <QQuickItem>
class ItemWithWindow : public QQuickItem
{
Q_OBJECT
public:
Q_PROPERTY( QQuickWindow* window READ window NOTIFY windowChanged )
signals:
void windowChanged();
};
(which gets registered as usual with qmlRegisterType<ItemWithWindow>( uri, 1, 0, "ItemWithWindow" );)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With