[Qt-qml] Extract RGB values from color

michael.brasser at nokia.com michael.brasser at nokia.com
Tue Oct 26 02:36:43 CEST 2010


Hi,

On 26/10/2010, at 12:02 AM, ext Andrew.Christian at nokia.com wrote:
> Is there any way to quickly extract R,G, and B values from color property? 
> 
> I tried the obvious things like:
> 
> Item {
>   property color myColor: "blue"
> 
>   function getRed() {
>      return myColor.red();
>   }
> }
> 
> but I don't get anything useful.  It looks like a QML color is exactly an underlying QColor object, but the QColor object isn't exposing any of the internal color information in a way that I can reach it from QML.


Unfortunately there isn't any easy, automatic way to do this as the moment. QML sees and manipulates color as a single value, rather than as an object with properties (QColor itself doesn't give any "object" interface -- e.g. it is not a QObject -- which is why this doesn't automatically work from QML). QML adds object-like support for some other simple value types (such as QRect, QFont, etc), so it should be possible to add similar support for QColor (could you add a suggestion to http://bugreports.qt.nokia.com if you'd like to see this added?).

If you only need R,G, and B (and not A), you might be able to use a technique like that that described at http://ejohn.org/blog/numbers-hex-and-colors/ to extract the values. Alternatively, you could expose some helper functions from C++ to do the conversions for you, for example

Q_INVOKABLE int getRed(const QColor &color) { return color.red(); }

Regards,
Michael



More information about the Qt-qml mailing list