[Development] Qt 6.2 QQmlMetaType incompatibility
Uwe Rathmann
Uwe.Rathmann at tigertal.de
Wed Oct 6 17:52:11 CEST 2021
Hi Ulf,
thanks for the hints.
> instead of using QQmlMetaType::registerCustomStringConverter() you > can add a static create() function that takes a QJSValue as argument
> to your class or have a ctor that takes QJSValue. Then register the >
type using QML_ANONYMOUS and it should be possible to create it from >
any JavaScript value assigned to it.
Couldn't get something running with this approach.
But as I'm using my class as property and the docs state that this
is not possible with classes being declared as QML_ANONYMOUS I gave up.
> You can also do a number of interesting and nasty things to wrap the > existing QMarginsF into a value type. Take a look at >
qquickvaluetypes_p.h in qtdeclarative for "inspiration". > > The only
thing shown there that you cannot do to your own value > types is giving
them names. The names passed to QML_VALUE_TYPE() are > only decoration
so far because the actual name resolution for value > types is still
hardcoded.
Not sure if I 100% understood this concept. Isn't it more about
declaring something in QML than using C++ classes from QML ?
Being able to provide a static create function would indeed be a good
solution:
QVariant QskMargins::create( const QJSValue ¶ms )
{
if ( ... )
{
const auto m = params.toNumber();
return QVariant::fromValue( QskMargins( m ) );
}
...
}
But how to tell the engine to make use of it when executing a property
assignment ?
With Qt < 6.2 it was as simple as that:
QQmlMetaType::registerCustomStringConverter( qMetaTypeId< QskMargins
>(),
[]( const QString& s ) { return QVariant::fromValue(
QskMargins( s.toDouble() ) );
Maybe it is possible to reintroduce something similar ?
Thanks,
Uwe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20211006/092975db/attachment.html>
More information about the Development
mailing list