Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Window from Item

Tags:

qt

qml

qtquick2

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.

like image 405
cmannett85 Avatar asked Oct 28 '13 23:10

cmannett85


2 Answers

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

like image 65
Maxim Paperno Avatar answered Nov 16 '22 19:11

Maxim Paperno


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

like image 43
Ivan Kolev Avatar answered Nov 16 '22 17:11

Ivan Kolev