[Interest] [Qt6] QML automatic type registration in a (shared) library

Konstantin Shegunov kshegunov at gmail.com
Fri Jan 14 16:05:21 CET 2022


On Fri, Jan 14, 2022 at 6:16 AM Ulf Hermann <ulf.hermann at qt.io> wrote:

> Is the place where the module's qmldir ends up reachable via the QML
> import path?


I don't know. I have the generated qmldir in
the C:\Programming\tsc\build\debug\TscUi directory, which I think should be
correct. I haven't messed with the output paths at all.
The application goes to a neighboring directory
in C:\Programming\tsc\build\debug\TscApp

I can see that the .rcc contains a couple of cpp files corresponding to the
qml files I have: TscUi_Main_qml.cpp, TscUi_BottomBar_qml.cpp
and TscUi_qmlcache_loader.cpp, but the resource path doesn't include any of
this (perhaps correctly). In any case it does not include the qmldir either.


> If you're linking the library into the application, you may
> just add :/ to your import path so that it's loaded from the qrc file
> system right away.


Yes, that's the first thing I tried, however when it didn't work I
inspected the resource path:

QDir dir(":/Tsc");

const auto list = dir.entryInfoList();

for (const auto & entry : list)

    qDebug() << entry;
Which produced only:
QFileInfo(:\Tsc\Core)


This is the other library, which exports C++ symbols into QML, and which as
mentioned works just fine. There's no path for this TscUi target, as I'd've
expected.

Be aware that some linkers just drop the linkage if
> they perceive it to be "unused", though.


Yes, perhaps this is the problem, but how do I fix this? What I currently
do in the application is to do nothing but link against:

qt_add_executable(TscApp

    main.cpp

)


target_compile_definitions(...)


target_link_libraries(TscApp

    PRIVATE Qt6::Core Qt6::Gui Qt6::Quick Qt6::Qml Tsc::Core Tsc::Ui

)


qt_add_resources(TscApp

    "tsc_main"

    PREFIX /

    FILES

       main.qml

)

main.qml here I use just as an entry point that I load with:

QQuickView view;

view.setResizeMode(QQuickView::SizeRootObjectToView);

view.setSource(u"qrc:/main.qml"_qs);
The qml content is just a trivial instantiation:
import Tsc.Ui

Main {}


Otherwise, if the module ends
> up in C:/foo/bar/Tsc/Ui/, then you need to add C:/foo/bar/ to the import
> path.
>

Yes, that's where it ends up, at least that's where the qmldir resides.

The executable would be at the root of the application-specific import
> path if you were following the recommended directory layout.
>

I thought I was, but I'm not sure anymore. The application is placed in:
<build-path-root>/TscApp
The library is in:
<build-path-root>/TscUi

I've done nothing to change the default layout.

On Fri, Jan 14, 2022 at 6:42 AM Günter Schwann <guenter at vikingsoftware.com>
wrote:

> Hi,
>
> I did come across some similiar issue. And my solution was to add the
> resource as import path.
>

Hi,
I did try this, but this doesn't work as the resource file doesn't contain
the qmldir to begin with (see above).
I must've messed up something else.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20220114/6870f38a/attachment.htm>


More information about the Interest mailing list