[Interest] using a C++ class in QML in Qt6

Dirk Hohndel dirk at hohndel.org
Sat Aug 31 23:06:43 CEST 2024


Yeah, I need one place where I can load my "main" QML file, and I need a way to make my C++ classes visible from within those QML files.
So I'm not sure how I'd do that with only one "load"...

In Qt5 I used the "load" to get the main.qml file and then RegisterType to get my C++ classes visible inside the QML code.
In your example, how do you load your QML file?

/D

> On Aug 31, 2024, at 14:03, ekke <ekke at ekkes-corner.org> wrote:
> 
> Hi Dirk,
> in my apps ATM I'm only using one qt qml module, so I'm doing engine.loadFromModule(...)
> see https://t1p.de/ekkeCMakeQMLModule
> 
> Have no experiences yet if you have to deal with more modules. Later will modularize my apps, much other refactorings must be done before ;-)
> 
> but you should not do multi engine.loadFrom...
> 
> good night...
> 
> ekke
> 
> Am 31.08.24 um 22:53 schrieb Dirk Hohndel via Interest:
>> Hi Ekke,
>> 
>> Thanks so much for the quick response.
>> 
>> This is a massive project (Subsurface-mobile) that we build with CMake.
>> 
>> Thanks for the link to your site with an explanation... this feels like I'm **ALMOST** there... 
>> 
>> I had already added QML_ELEMENT to the class header.
>> 
>> In the existing code, we load the main.qml from the resource system with
>> 
>> engine.load(QUrl(QStringLiteral("qrc:///qml/main.qml")));
>> 
>> I added a second line
>> 
>> engine.loadFromModule("subsurface/mobile/main", "QMLProfile");
>> 
>> Rebuilt things and get:
>> 
>> INFO: QQmlApplicationEngine failed to load component
>> INFO: qrc:/qml/main.qml:8:1: module "subsurface.mobile.main" is not installed
>> INFO: QQmlApplicationEngine failed to load component
>> INFO: <Unknown File>: No module named "subsurface/mobile/main" found
>> 
>> Do I need to do anything else to ensure that the type is visible and subsequently the module is found?
>> 
>> Thanks again for your help!
>> 
>> /D
>> 
>>> On Aug 31, 2024, at 12:56, ekke <ekke at ekkes-corner.org> <mailto:ekke at ekkes-corner.org> wrote:
>>> 
>>> What is your build system ? QMake or CMake ?
>>> 
>>> Instead of qmlRegisterType you should use QML_ELEMENT
>>> see https://t1p.de/ekkeQML_ELEMENT
>>> 
>>> ekke
>>> 
>>> Am 31.08.24 um 21:23 schrieb Dirk Hohndel via Interest:
>>>> 
>>>> Hi there,
>>>> 
>>>> I've been trying to figure out how to port a QML app from Qt5 to Qt6 and have been unable to solve what seems like a fairly basic issue.
>>>> 
>>>> In the Qt5 version, I was able to make a class available from C++ by simply calling
>>>> 
>>>> ...
>>>> qmlRegisterType<QMLProfile>("org.subsurfacedivelog.mobile", 1, 0, "QMLProfile)
>>>> ...
>>>> 
>>>> in the C++ code and then using
>>>> 
>>>> import org.subsurfacedivelog.mobile
>>>> 
>>>> in the QML file in order to then be able to use that type as
>>>> 
>>>> QMLProfile {
>>>> ...
>>>> }
>>>> 
>>>> in the QML code.
>>>> 
>>>> When I try this code unmodified, it compiles fine, but at run time tells me that it cannot find the org.subsurfacedivelog.mobile module when it encounters that import org.subsurfacedivelog.mobile line.
>>>> 
>>>> So I tried some searching and found several pages in the Qt6 documentation that all seem to imply that this isn't how things should be done in Qt6 and that instead I would need to go down the route of defining a separate QML module with its own compile unit (is this true? why would you disable this core feature???)
>>>> 
>>>> And then trying to do that, I fail to get the module to compile with missing include paths.
>>>> If it turns out that that's the only way to go, I'll happily give a lot more details on this issue -- but I'd love to first get back to the previous approach.
>>>> Is there still a way in Qt6 to register a Type with QML without having to create separate modules?
>>>> 
>>>> Thanks
>>>> 
>>>> /D
>>>> 
>>>> 
>>>> _______________________________________________
>>>> Interest mailing list
>>>> Interest at qt-project.org <mailto:Interest at qt-project.org>
>>>> https://lists.qt-project.org/listinfo/interest
>>> 
>>> _______________________________________________
>>> Interest mailing list
>>> Interest at qt-project.org <mailto:Interest at qt-project.org>
>>> https://lists.qt-project.org/listinfo/interest
>> 
>> 
>> 
>> _______________________________________________
>> Interest mailing list
>> Interest at qt-project.org <mailto:Interest at qt-project.org>
>> https://lists.qt-project.org/listinfo/interest
> 
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> https://lists.qt-project.org/listinfo/interest

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20240831/13580aa1/attachment.htm>


More information about the Interest mailing list