[Development] Another method of registering QML types

Alan Alpert 416365416c at gmail.com
Thu Nov 8 18:57:05 CET 2012


Currently, there is no way to register QML files as types from C++.
This is the exact same functionality that qmldir provides, but I think
there are situations where you'll want to do this progamatically
instead of with a qmldir file. There is a very specific example I have
in mind: Platform Components.

I'm suggesting another qmlRegisterTypes function, one that takes a URL
instead of a C++ type:

qmlRegisterType(const char* url, const char *uri, int versionMajor,
int versionMinor, const char *qmlName).

https://codereview.qt-project.org/#change,39135 is a proof of concept
implementation.

This would allow for a platform component import which looks like this:
if(platform=="desktoplinux")
    qmlRegisterType("/usr/share/desktop/components/Button.qml", uri,
2, 0, "Button");
else if (platform=="meego")
    qmlRegisterType("/usr/share/meego/components/Button.qml", uri, 2,
0, "Button");
...

Except that the strings would also be generated procedurally, instead
of having a dozen lines of code per component type.

The function isn't strictly for that usecase, there are other cases
where you might want dynamically directed types. The other case that
comes to mind is if you have an application with built-in types and
you want to add a file from qrc as a type to outside files (or
vice-versa). It would be nice to hear other usecases if anyone has
them, to ensure this API can meet those requirements.

Any comments on this suggested addition to the QtDeclarative public API?

Since this is adding to public API, I have a bigger question. Where
does this change go? I did the PoC in Qt 4.8 because it was easier, is
there a branch for 4.9/master that I didn't see in the 4.x series? Or
does that no longer get functionality additions, and this should be
going into 5.x only?

--
Alan Alpert



More information about the Development mailing list