GroupBox中MouseArea的父级

时间:2015-07-21 12:55:43

标签: qt qml qtquick2 qqmlcomponent

MouseArea中的GroupBox的父级是什么? parent引用了一些container

GroupBox {
    width: 100; height: 100
    id: rec
    MouseArea {
        anchors.fill: parent
        acceptedButtons: Qt.LeftButton | Qt.RightButton
        onClicked: {
            console.log("\tparent" + parent + "\trec" + rec)
        }
    }
}
  

qml:parentQQuickItem_QML_15(0x3ad3590,"容器")recGroupBox_QMLTYPE_12(0x3ad2790)

MouseArea中的RectangleRectangle是其父级时:

Rectangle {
    width: 100; height: 100
    id: rec
    color: "green"
    MouseArea {
        anchors.fill: parent
        acceptedButtons: Qt.LeftButton | Qt.RightButton
        onClicked: {
            console.log("\tparent" + parent + "\trec" + rec)
        }
    }
}
  

qml:parentQQuickRectangle(0x39d0cd0)recQQuickRectangle(0x39d0cd0)

1 个答案:

答案 0 :(得分:2)

在大多数QML控件(和窗口)中,内部项占据控件本身的所有子项是常见的行为。它适用于WindowScrollViewFlickable甚至GroupBox。这样的组件可以作为属性(通常)称为contentItem

如果您编写一个打印此类属性的示例,您会看到contentItem是您要搜索的父级:

import QtQuick 2.4
import QtQuick.Controls 1.3

ApplicationWindow {
    id: container

    width: 640
    height: 480
    visible: true

    property int clicksCounter: 0

    GroupBox {
        width: 100; height: 100
        id: rec
        MouseArea {
            anchors.fill: parent
            acceptedButtons: Qt.LeftButton | Qt.RightButton
            onClicked: {
                console.log("\tparent " + parent + "\tcontentItem " + rec.contentItem)
            }
        }
    }
}

输出:

  

qml:parent QQuickItem_QML_26( 0x1411fc0 ,"容器")contentItem QQuickItem_QML_26( 0x1411fc0 ,"容器")