Question

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)}
    }
}
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top