如何将弹出窗口添加到嵌套项目内的根应用程序窗口?

Ros*_*ers 4 qt qml

我想创建一个覆盖整个 root 的Popup模态ApplicationWindow,但我想Popup在我正在构建的小部件容器中实例化它,该容器将深深嵌套在 QML 层次结构中。不幸的是,实例化Popup不是 的直接子级ApplicationWindow并且滚动到视图之外的 不会居中并覆盖ApplicationWindow

在下面的代码中,我Popup在 child 中实例化了 a Item,但我不知道如何使其在视口上居中:

import QtQuick 2.9
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3

ApplicationWindow {
  id: root;
  visible: true
  width: 100
  height: 150

  Flickable {
    id: flickable;
    contentHeight: col_layout.height;
    anchors.fill: parent;
    ColumnLayout {
      id: col_layout;
      Rectangle {color:'green'; width:100; height: 100}
      Rectangle {color:'red';   width:100; height: 100;
        Popup{
          id: popup;
          modal: true;
          visible: true;
          Rectangle{ color:'blue'; width:200; height: 200; }
        }
      }
    }
    MouseArea {
      anchors.fill: parent;
      onClicked: {
        popup.open();
        popup.visible = true;
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这会产生(蓝色Rectangle是模态内容):

在此输入图像描述

我是否必须将 a 放在Popupthe 的全局范围内ApplicationWindow,或者是否有某种方法可以Popup在某些子项Item或模块中实例化和控制 the ?

我尝试过设置parent属性,Popuproot没有效果。我真的很想避免将所有可以想象到的弹出窗口都塞进 root 中ApplicationWindow

der*_*erM 5

您无法将Popupto的父级设置为root,因为root它不是Item- 类型。因此不可能是这样parent

但是,您可以将其作为父级root.contentItem

Popup由于您可能会在其他文件中声明,因此可能会隐藏id: root,因此我会投票支持使用附加属性ApplicationWindow,如果您使用它,则该附加属性是可用的。

Popup {
    id: popup;
    modal: true;
    visible: true;
    parent: ApplicationWindow.contentItem // This will do the trick
    Rectangle{ color:'blue'; width:200; height: 200; }
}
Run Code Online (Sandbox Code Playgroud)