7
votes

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.

2
@FrankOsterfeld Thanks, but was I curious from the QML side.cmannett85

2 Answers

7
votes

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

3
votes

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" );)