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

Dirk Hohndel dirk at hohndel.org
Sat Aug 31 22:53:53 CEST 2024


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> 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
> https://lists.qt-project.org/listinfo/interest

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


More information about the Interest mailing list