【问题标题】:Draggable item get hidden behind other UI elements - Qt Qml可拖动项目隐藏在其他 UI 元素后面 - Qt Qml
【发布时间】:2017-12-09 17:05:05
【问题描述】:

在我的应用程序窗口中,我有两个 ItemPanels,它们在 RawLayout 内组织。

Item {
    RowLayout {
        anchors.fill: parent
        spacing: 1

        ItemPanel {
            Layout.preferredWidth: 250
            Layout.fillHeight: true

            panelHeader: "Components"

            DraggableItem {
                width: 100; height: 100;
            }
        }


        ItemPanel {
            Layout.preferredWidth: 250
            Layout.fillHeight: true

            panelHeader: "Actions"
        } 
    }
}

Components 面板包含DraggableItems。可拖放区域是Actions 面板。但如果我试图将DraggableItem 拖到Actions 面板,在我拖动它时它会被隐藏。

可拖动项目

Item {
    id: root
    property string colorKey: "red"

    width: 64; height: 64

    MouseArea {
        id: mouseArea

        width: 64; height: 64
        anchors.centerIn: parent

        drag.target: tile

        onReleased: parent = tile.Drag.target !== null ? tile.Drag.target : root

        Rectangle {
            id: tile

            width: 64; height: 64
            anchors.verticalCenter: parent.verticalCenter
            anchors.horizontalCenter: parent.horizontalCenter

            color: colorKey

            Drag.keys: [ colorKey ]
            Drag.active: mouseArea.drag.active
            Drag.hotSpot.x: 32
            Drag.hotSpot.y: 32
            states: State {
                when: mouseArea.drag.active
                ParentChange { target: tile; parent: root }
                AnchorChanges { target: tile; anchors.verticalCenter: undefined; anchors.horizontalCenter: undefined }
            }
        }
    }
}

我正在考虑添加一个透明图层并在拖动时将父图层更改为它,但我不知道如何在所有内容之上添加一个新图层。还有其他解决方法吗?

【问题讨论】:

  • 您是否尝试过与DraggableItemz property 发生性关系,然后两者都大于ItemPanels?
  • @folibis:ItemPanel“组件”的 z 值应该更大。其子代的 z 值不太相关,除非它们重新成为ItemPanels 的兄弟姐妹。
  • 将 z 值添加到 ItemPanel 不起作用。我为第一个 ItemPanel 添加了 z:1,在 Actions 面板中添加了 200。
  • 当然,如果Actions-panel的z更大,它会再次失败。对象的z-value 越高,它的分层就越高 - 相对于兄弟姐妹。如果 z 值相同,则最后添加的将叠加在顶部。负值将使对象在父对象之下分层。 z 值始终只与兄弟姐妹相关。

标签: qt drag-and-drop qml


【解决方案1】:

最简单的方法是将带有panelHeader: "Components"ItemPanelz 值更改为大于兄弟姐妹的值。然后自动地,所有的孩子也将在他们之上分层。

重生也是一个很好的解决方案。如果您在项目中使用 ApplicationWindow 作为根对象,您甚至不需要创建添加新层 - 它已经存在。

您可以使用 附加属性 ApplicationWindow 将您的可拖动对象重新设置为 ApplicationWindow.overlay。剩下的唯一挑战是,您需要指定正确的位置。您可以为此使用mapToItem()mapFromItem()

如果您没有ApplicationWindow 作为根,您可以添加一个常规Item 作为您的根的子级,并将z-值设置为它的兄弟姐妹中的最大值。然后你给它一个id。如果您不隐藏此id(其他标识符具有相同名称,以更高的优先级解析),您可以从任何地方寻址此Item。请参阅此here 的更多信息。

【讨论】:

  • <applicationWindowId>.overlay 作为父母工作。我也可以只使用overlay 这个词作为父母。我没有得到mapToItem() 的东西。为什么我需要那个?
猜你喜欢
  • 1970-01-01
  • 2018-11-06
  • 2013-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多