trying to create list (docs seem in error)
-
Hi all -
I'm trying to create a list, but am getting errors. I tried the code in the pic below -- this came straight from the docs:
Here's how I'm trying to use it - I may have my own errors in how I'm trying to use color:property list<color> rectColors: [Qt.red, Qt.green, Qt.blue, Qt.yellow, Qt.orange] Repeater { id: rects model: rectColors Rectangle { width: mainWindow.width height: 100 color: rectColors[index] } }
Any ideas on what's going on here? Thanks...
-
@mzimmers What @JoeCFD said is only true for Qt 5. In Qt 6 in can also hold basic value types as mentioned by the doc you linked.
Qt Creator is bit confused in its linting but the code actually runs.
There is a problem in your code though.
Qt.red
and its friends are not actual colors but enums, you can't cast them tocolor
.You could use a QColor from C++,
Qt.rgba()
, a SVG color name or hex color code:
property list<color> rectColors: ["red", "green", "blue", "yellow", "orange"]
-
Hi all -
I'm trying to create a list, but am getting errors. I tried the code in the pic below -- this came straight from the docs:
Here's how I'm trying to use it - I may have my own errors in how I'm trying to use color:property list<color> rectColors: [Qt.red, Qt.green, Qt.blue, Qt.yellow, Qt.orange] Repeater { id: rects model: rectColors Rectangle { width: mainWindow.width height: 100 color: rectColors[index] } }
Any ideas on what's going on here? Thanks...
-
- so, the doc is wrong?
- according to the link you provided, lists can only store QML objects. That would seem to imply that I can't create a list of colors...true?
-
@mzimmers
https://doc.qt.io/qt-5/qml-list.html
A list can only store QML objects, and cannot contain any basic type values.
(To store basic types within a list, use the var type instead.) -
M mzimmers has marked this topic as solved on
-
@mzimmers What @JoeCFD said is only true for Qt 5. In Qt 6 in can also hold basic value types as mentioned by the doc you linked.
Qt Creator is bit confused in its linting but the code actually runs.
There is a problem in your code though.
Qt.red
and its friends are not actual colors but enums, you can't cast them tocolor
.You could use a QColor from C++,
Qt.rgba()
, a SVG color name or hex color code:
property list<color> rectColors: ["red", "green", "blue", "yellow", "orange"]
-
@mzimmers
https://doc.qt.io/qt-5/qml-list.html
A list can only store QML objects, and cannot contain any basic type values.
(To store basic types within a list, use the var type instead.)@JoeCFD said in trying to create list (docs seem in error):
A list can only store QML objects, and cannot contain any basic type values.
Not true since Qt 6.4. See https://doc.qt.io/qt-6/qml-list.html#using-the-list-type
(To store basic types within a list, use the var type instead.)
Using
var
prevents your QML code from getting compiled to C++ (see https://www.qt.io/blog/the-new-qtquick-compiler-technology ). Always prefer the specific type where possible.@mzimmers said in trying to create list (docs seem in error):
- so, the doc is wrong?
No, Qt Creator is wrong: https://bugreports.qt.io/browse/QTCREATORBUG-28238 It will be fixed in Qt Creator 10.0 (the beta is currently available from the online installer)
Your code will run fine with Qt 6.4 and newer.
- according to the link you provided, lists can only store QML objects. That would seem to imply that I can't create a list of colors...true?
False 😊 But @GrecKo is right: Use
"red"
instead ofQt.red
-
@JoeCFD said in trying to create list (docs seem in error):
A list can only store QML objects, and cannot contain any basic type values.
Not true since Qt 6.4. See https://doc.qt.io/qt-6/qml-list.html#using-the-list-type
(To store basic types within a list, use the var type instead.)
Using
var
prevents your QML code from getting compiled to C++ (see https://www.qt.io/blog/the-new-qtquick-compiler-technology ). Always prefer the specific type where possible.@mzimmers said in trying to create list (docs seem in error):
- so, the doc is wrong?
No, Qt Creator is wrong: https://bugreports.qt.io/browse/QTCREATORBUG-28238 It will be fixed in Qt Creator 10.0 (the beta is currently available from the online installer)
Your code will run fine with Qt 6.4 and newer.
- according to the link you provided, lists can only store QML objects. That would seem to imply that I can't create a list of colors...true?
False 😊 But @GrecKo is right: Use
"red"
instead ofQt.red
-
M mzimmers has marked this topic as unsolved on
-
Thanks for the clarification on this. I downloaded Creator 10 and as @JKSH said, the error disappeared. Here's the working code:
Column { Repeater { id: repeater property list<color> rectColors: ["red", "green", "blue", "yellow", "orange"] model: rectColors Rectangle { width: mainWindow.width height: 100 color: repeater.rectColors[index] } } }
Seems to work fine. Now I can ask my real question (in another topic). Thanks again.
-
M mzimmers has marked this topic as solved on
-
You are not using
Repeater
the intended way. You are not supposed to acces the raw model from the delegates, let the Repeater provide you the data.Change
color: repeater.rectColors[index]
with
color: modelData