[Qt-qml] Custom QML types and strings
michael.brasser at nokia.com
michael.brasser at nokia.com
Thu Dec 29 02:18:20 CET 2011
On 29/12/2011, at 1:22 AM, ext Harri Pasanen wrote:
> I wrote my custom QML type, using Qt 4.7.4.
> It is working just fine, except when I run using it I keep getting
> messages like:
>
> QColor::setNamedColor: Unknown color name 'mpaja.lvl'
>
>
> Comes from here:
>
>
> void QColor::setNamedColor(const QString &name)
> {
> if (!setColorFromString(name))
> qWarning("QColor::setNamedColor: Unknown color name '%s'",
> name.toLatin1().constData());
> }
>
>
> I traced it a bit, and it seems to be triggered from
>
> QVariant QDeclarativeStringConverters::variantFromString(const QString &s)
> {
> if (s.isEmpty())
> return QVariant(s);
> bool ok = false;
> QRectF r = rectFFromString(s, &ok);
> if (ok) return QVariant(r);
> QColor c = colorFromString(s, &ok); <<<=== Here
> if (ok) return QVariant(c);
> QPointF p = pointFFromString(s, &ok);
> if (ok) return QVariant(p);
> QSizeF sz = sizeFFromString(s, &ok);
> if (ok) return QVariant(sz);
> QVector3D v = vector3DFromString(s, &ok);
> if (ok) return qVariantFromValue(v);
>
> return QVariant(s);
> }
>
> I'm guessing that when ever a parameter is passed from QML to C++ it
> gets converted to QVariant, which makes a best guess at the underlying type.
>
> But I'm a bit puzzled why only some of the strings elicit this warning.
>
> My custom type looks like:
>
> Spoke { name: "MPaja"; value: "mpaja.lvl" }
>
> and I'm getting a warning for each "value:", but not for "name:"
>
> Is there some naming convention or other method that I should be aware
> off to avoid this needless warning?
Hi Harri,
This issue should be fixed for 4.8 (https://bugreports.qt.nokia.com/browse/QTBUG-19098), if you are able to switch to that. For 4.7.4 the only way I know of to work around it is to make things more strongly typed when possible -- e.g. in your above example, if the "value" property can be of type QString (or QUrl, etc) rather than QVariant, you should be able to avoid the warning.
Regards,
Michael
More information about the Qt-qml
mailing list