I'm using Qt 5.7 on Windows 10 and I'm relatively new to QML. I am currently adding some colors to my application and it struck me that there seems to be no way of using the plain integer RGB values (not hexadecimals) for colors. There are (as far as I know) three direct ways (without using the Qt C++ functions of QColor
in combination with the QML
code) of setting an item to a specific color:
Qt.rgba(...)
- there are two problems I'm having with this the first one being the usage of floating point numbers for the RGB
values and the second one being the weird call - I find myself forgetting quite often that I have to put Qt.
in front of the function and needless to say this is annoying (but this is just me :P):
Rectangle {
width: 100
height: 100
color: Qt.rgba(0.502, 0.502, 0.502, 1)
border.width: 1
}
#
- again I have two problems here the first one being that obvious inconsistency if we compare it to the Qt.rgba(...)
function in terms of placement of the alpha
value (here in front and not at the end like the last argument of the Qt.rgba(...)
) and the second one being the usage of hexadecimals:
Rectangle {
width: 100
height: 100
color: "#FF808080"
border.width: 1
}
color names - this is nice (perhaps) if English is your native language and you are familiar with the vast amount of color names available in Qt (which uses the SVG color naming):
Rectangle {
width: 100
height: 100
color: "grey"
border.width: 1
}
All three coloring conventions above represent the same color (RGB
: 128, 128, 128). I really prefer using the normal integer values since I'm more familiar with those. Is it possible to use those without any extra effort in QML?
No.
Building off what @Gwen said, you can define your own function that takes integers and does the conversion to floats, and then call that everywhere.