当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, "container") recgroupbox_qmlype_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)
在大多数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, "container") contentItem QQuickItem_QML_26(0x1411fc0, "container")