qt - ReferenceError: down is not defined in SpinBox QML -
i referring https://doc.qt.io/qt-5/qtquickcontrols2-customize.html#customizing-spinbox , copy pasted same code, getting following errors:
referenceerror: down not defined , referenceerror: not defined.
code:
import qtquick 2.6 import qtquick.controls 2.1 spinbox { id: control value: 50 editable: true contentitem: textinput { z: 2 text: control.textfromvalue(control.value, control.locale) font: control.font color: "#21be2b" selectioncolor: "#21be2b" selectedtextcolor: "#ffffff" horizontalalignment: qt.alignhcenter verticalalignment: qt.alignvcenter readonly: !control.editable validator: control.validator inputmethodhints: qt.imhformattednumbersonly } up.indicator: rectangle { x: control.mirrored ? 0 : parent.width - width height: parent.height implicitwidth: 40 implicitheight: 40 color: up.pressed ? "#e4e4e4" : "#f6f6f6" border.color: enabled ? "#21be2b" : "#bdbebf" text { text: "+" font.pixelsize: control.font.pixelsize * 2 color: "#21be2b" anchors.fill: parent fontsizemode: text.fit horizontalalignment: text.alignhcenter verticalalignment: text.alignvcenter } } down.indicator: rectangle { x: control.mirrored ? parent.width - width : 0 height: parent.height implicitwidth: 40 implicitheight: 40 color: down.pressed ? "#e4e4e4" : "#f6f6f6" border.color: enabled ? "#21be2b" : "#bdbebf" text { text: "-" font.pixelsize: control.font.pixelsize * 2 color: "#21be2b" anchors.fill: parent fontsizemode: text.fit horizontalalignment: text.alignhcenter verticalalignment: text.alignvcenter } } background: rectangle { implicitwidth: 140 border.color: "#bdbebf" } }
should include additional imports ?
no, don't need include additional imports. just, example uses bad practice , here see why:
this (reduced modifying up.indicator) code of example:
import qtquick 2.6 import qtquick.controls 2.0 spinbox { id: control value: 50 editable: true up.indicator: rectangle { x: control.mirrored ? 0 : parent.width - width height: parent.height implicitwidth: 40 implicitheight: 40 color: up.pressed ? "#e4e4e4" : "#f6f6f6" // <---* border.color: enabled ? "#21be2b" : "#bdbebf" text { text: "+" font.pixelsize: control.font.pixelsize * 2 color: "#21be2b" anchors.fill: parent fontsizemode: text.fit horizontalalignment: text.alignhcenter verticalalignment: text.alignvcenter } } }
here have spinbox
root element of file.
now lets talk name resolution.
line in question marked // <---*
control: up.pressed ? "..." : "..."
where come from? @ first in object, used - rectangle
. rectangle
has no up
-property, continue , looking in root node of file, spinbox
- has up
-property, has value pressed
.
the situation looks different, when try use (the wrong way). let's add applicationwindow
:
import qtquick 2.7 import qtquick.window 2.2 import qtquick.controls 2.0 applicationwindow { id: window width: 800 height: 600 visible: true spinbox { id: control value: 50 editable: true up.indicator: rectangle { x: control.mirrored ? 0 : parent.width - width height: parent.height implicitwidth: 40 implicitheight: 40 color: up.pressed ? "#e4e4e4" : "#f6f6f6" border.color: enabled ? "#21be2b" : "#bdbebf" text { text: "+" font.pixelsize: control.font.pixelsize * 2 color: "#21be2b" anchors.fill: parent fontsizemode: text.fit horizontalalignment: text.alignhcenter verticalalignment: text.alignvcenter } } } }
same line - try up
in rectangle
, fails , continues files root node, applicationwindow
. here fails again. applicationwindow
not used in file, files root node might exist, search end , fail.
what went wrong? writer of example missed apply practice use qualified identifiers: id.property....
did example define x
-value of rectangle
.
changing line:
color: up.pressed ? [...]
to:
color: control.up.pressed ? [...]
will solve problem, explicitly declare property up
.
important lesson
- names not resolved going child parent, child files root node , on, until can't go on
- always identify object either explicitly
parent
(but aware, might not know parent is. ideally anchors , position) or objects'sid
Comments
Post a Comment