从物品中获取窗口

问题描述 投票:5回答:2

在QML中有没有办法获得任何视觉对象的顶级Window?最好不要通过视觉parent层次结构递归。

我正在尝试找到顶层窗口的几何体,因此后代对象可以检测它们的边界是否越过了窗口。

qt qml qtquick2
2个回答
3
投票

我想现在的答案是“不”。这看起来像是一个可以发送给QML团队的功能建议。

我最终将自己的C ++类导出到QML。

ItemWithWindow.h:

#include <QQuickItem>

class ItemWithWindow : public QQuickItem
{
    Q_OBJECT

public:

    Q_PROPERTY( QQuickWindow* window READ window NOTIFY windowChanged )

signals:

    void windowChanged();
};

(通常与qmlRegisterType<ItemWithWindow>( uri, 1, 0, "ItemWithWindow" );注册)


2
投票

所有Windows都附有Item属性。哪些依赖于Qt版本。例如。 Window.width是当前顶级窗口/视图宽度。

你可以用Item得到一个特定的WindowmyItem.Window;

使用Qt 5.7+,您甚至可以通过Window访问所有Window.window属性。

见文档:http://doc.qt.io/qt-5/qml-qtquick-window-window.html#attached-properties

© www.soinside.com 2019 - 2024. All rights reserved.