문제

I have just this simple qml application, that should output background color, when is clicked. Actually it still outputs undefined.

import QtQuick 2.1
import QtQuick.Controls 1.0

ApplicationWindow {
    title: qsTr("Hello World")
    width: 1400
    height: 800
    color: "#414141"

    menuBar: MenuBar {
        Menu {
            title: qsTr("File")
            MenuItem {
                text: qsTr("Exit")
                onTriggered: Qt.quit();
            }
        }
    }

    MouseArea {
        anchors.fill: parent
        onClicked: {console.log(parent.color)}
    }
}
도움이 되었습니까?

해결책

I am not sure exactly why but it looks like ApplicationWindow is not directly the parent of your MouseArea. You can check this by adding an id to your ApplicationWindow like root and then changing your onClicked event to this

onClicked: {
  console.log("MouseArea parent is: ", parent);
  console.log("root is: ", root);
}

I get this in the output which shows two different items

MouseArea parent is:  QQuickItem(0x101d369b0)
root is:  ApplicationWindow_QMLTYPE_7(0x101d35460)

To avoid this, just use console.log(root.color) instead of using parent

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top