[Interest] Problems with qt_add_qml_module

Stefan Seefeld stefan at seefeld.name
Wed Oct 25 06:54:17 CEST 2023


Hi,

allow me to follow up on my own mail. I have made some progress through
exploring (and reverse-engineering), and I would like to refine my
questions to refocus the discussion. But perhaps it's best if I briefly
describe my misunderstandings first, as that may help others as well.

The first argument to the `qt_add_qml_module()` function is *not* a new
target to be defined by the call. Rather, it is to be defined prior -
typically as a library - and it is considered to be the "backing target".
Furthermore, the `SOURCES` argument isn't a list of files that are to be
compiled into a new artefact (library or executable). Rather, it's a list
of files to be inspected by some Qt tools to extract types that are to be
exported through the newly defined module / plugin. (As such, the files
listed in `SOURCES` are typically also listed as sources to the backing
target assumed to be defined via the usual `add_library()`,
`target_link_libraries()`, etc.) It is also worth noting that for the
introspection only headers are needed, i.e. the `SOURCES` argument to
`at_add_qml_module()` only needs to list headers that define QML types
(those that use the `QML_ELEMENT`, typically).
Is this description accurate so far ?

There are a few caveats I observed: The call to `qt_add_qml_module()` has
to occur in the same CMakeLists.txt file as the associated backing target
definition (e.g. `add_library()`), or else make complains about missing
symbols. Further, the `OUTPUT_DIRECTORY` argument to `qt_add_qml_module()`
seems to be required (at least if more than one QML modules are defined).

It is probably assumed that the structure of the QML modules matches the
structure of the underlying C++ libraries. That is not quite the case for
me. I want to create a set of QML modules whose types come from a variety
of C++ libraries, without a simple one-to-one mapping between C++ library
and QML module. One thing that bothers me in particular is that The classes
that I want to reflect into QML are supposed to contain the `QML_ELEMENT`
macro, which I think is a bit intrusive. I would prefer if these classes
wouldn't have to know about QML at all, i.e. all the meta-object
infrastructure would be added separately (as with the `qmlRegisterType<>()`
business). That would allow me to more clearly separate backend from
frontend.

All that being said, am I on the right track, or am I still fundamentally
misunderstanding how to use QML with Qt6 ?

Thanks,

On Tue, Oct 24, 2023 at 2:50 PM Stefan Seefeld <stefan at seefeld.name> wrote:

>
>
> On Tue, Oct 24, 2023 at 10:47 AM Ulf Hermann <ulf.hermann at qt.io> wrote:
>
>> > In our existing code we call `qmlRegisterType<...>(...)` in our regular
>> > C++ code (e.g., some shared libraries that the main application links
>> to).
>> > I thought that, to be able to use those types in stand-alone QML code,
>> > I'd need to move these calls into plugins, or at least have some QML
>> > extension module link to these libraries so they can see these types.
>> > What am I missing ?
>>
>> You are getting this fundamentally wrong. When using qt_add_qml_module
>> you do not have to call any qmlRegisterType at all. qmltyperegistrar and
>> CMake do all of this behind the scenes and relieve you from the pain of
>> maintaining your plugins, type registration calls, qmltypes files,
>> versions, metaobject revisions and all the other cruft you have to do in
>> Qt5. You can easily have modules that contain both QML files and
>> C++-based types now, too.
>>
>
> That sounds great. It would be nice if this was documented a little more
> clearly, though, as the existing docs are a little light and obscure.
> How does this mechanism figure out what types should be registered /
> exported and under what names ?
> As I described in my previous mail, we have a great number of libraries,
> defining many classes. Some of them derive from `QObject`,
> and a few of those need to be made available to QML.
> I expect to eventually create a number of QML extension modules (following
> the modular structure of my application), so how can I express which
> headers to extract the types from, and how can I define the names I want
> to give to those types in QML ?
>
> Thanks,
>
> --
>
>       ...ich hab' noch einen Koffer in Berlin...
>
>

-- 

      ...ich hab' noch einen Koffer in Berlin...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20231025/a3fa5263/attachment.htm>


More information about the Interest mailing list