cmannett85
cmannett85

Reputation: 22356

Get Window from Item

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.

Upvotes: 9

Views: 5328

Answers (2)

Maxim Paperno
Maxim Paperno

Reputation: 4869

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

Upvotes: 11

Ivan Kolev
Ivan Kolev

Reputation: 211

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

Upvotes: 3

Related Questions