Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

qml

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 }
    }
}
like image 593
Phrogz Avatar asked Jul 17 '17 22:07

Phrogz


1 Answers

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.

like image 166
Phrogz Avatar answered Nov 04 '22 00:11

Phrogz