Phrogz
Phrogz

Reputation: 303146

Invalid property assignment: "anchors" is a read-only property

Wrote a simple QML app, and it's complaining that I can't set the anchors for a text item.

qrc:/main.qml:13 Invalid property assignment: "anchors" is a read-only property

What's going on?

import QtQuick 2.7
import QtQuick.Controls 2.0

ApplicationWindow {
    visible: true
    width:320; height:240
    title: "Driver Confidence"

    property real accSpeed: 29.0576

    Text {
        text: accSpeed.toFixed(0) + " m/s"
        anchors: { top:parent.top; left:parent.left }
    }
}

Upvotes: 9

Views: 2226

Answers (1)

Phrogz
Phrogz

Reputation: 303146

The problem is a simple syntax error combined with a confusing error message. The anchors line should not have a colon after anchors:

anchors { top:parent.top; left:parent.left }

With the colon it is trying to evaluate the block as a JavaScript expression (which is invalid) and then assign the result to overwrite the entire anchors object.

Upvotes: 13

Related Questions