0

什么是父母MouseArea,什么时候在GroupBoxparent指一些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:父QQuickItem_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:父QQuickRectangle(0x39d0cd0)recQQuickRectangle(0x39d0cd0)

4

1 回答 1

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:父 QQuickItem_QML_26(0x1411fc0,“容器”)内容项 QQuickItem_QML_26(0x1411fc0,“容器”)

于 2015-07-21T13:06:00.907 回答