Static Library & use QML_ELEMENT to Expose the classes
-
Hi All
Qn - How to use the QObject classes bundled inside the static library using QML_ELEMENT.
Details of my issue -
- Linux Platform. Qt 6.x
- Built static library. It contains the follwing.
a. CarInfo - Uses QML_ELEMENT (no qmlregistertype)
b. TyreInfo - Uses QML_ELEMENT (no qmlregistertype)
c. MyStar.qrc - This contains the qml file(MyStyles.qml)
When I build the static library it generates the cpp file with following entry
Q_QMLTYPE_EXPORT void qml_register_types_CarInfo()
{
qmlRegisterTypesAndRevisions<CarInfo>("CarInfo", 1);
qmlRegisterTypesAndRevisions<TyreInfo>("CarInfo", 1);
qmlRegisterModule("CarInfo", 1, 0);
}I'm linking the static library with my applicaiton. In order to use the classes present in static library, method qml_register_types_CarInfo() should be explicitly called.
Is there anyother way to call the above method or something else ?
Since method should be explicitly called, I need to modify the signature to
extern "C" void qml_register_types_CarInfo(){}Is there any best practice when I bundle the classes in static library with QML_ELEMENT ?
-
Some additional inputs.
On windows with MingW compiler it behaves differently.
- On Windows extern "C" key is not required.
- I need to load the library explicitly using QLibrary both on Linux & Windows.
- Once I do an import in qml, qml_register_types_CarInfo(){} is called on by qmlengine.
Only extern "C" is required in Linux.
Is this a bug ? Anyone noticed this issue ?
-
Further investigation on this.
Some how we need to load the staticlibrary.- On Linux 6.4.3 Q_INIT_RESOURCE(MyLib). This automatically loaded the libary & called qml_register type.
- Windows 6.61 - Q_INIT_RESOURCE(MyLib) - This gave me linker error saying that qInitResource_MyLib() is not defined.
I just created the object from C++ class present in the library. It worked. - We don't have to modify with extern "C". Not required.