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
中的Rectangle
,Rectangle
是其父级时:
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)
答案 0 :(得分:2)
在大多数QML控件(和窗口)中,内部项占据控件本身的所有子项是常见的行为。它适用于Window
,ScrollView
,Flickable
甚至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 ,"容器")