Qt 6.9 and QML harmony?
-
Hi,
I just started to learn QML with Qt 6.9 and I see that some little harmony like problem?It's been lots of thing changed from Qt-5 to Qt-6 abot QML and C++ integration.
For example I can not see my cpp files definition in QML file?
My project tree is like below;I added in main cpp
int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); System m_systemHandler; QQmlApplicationEngine engine; QObject::connect( &engine, &QQmlApplicationEngine::objectCreationFailed, &app, []() { QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.loadFromModule("TeslaPanel", "Main"); QQmlContext * context(engine.rootContext()); context->setContextProperty("systemHandler", &m_systemHandler); return app.exec(); }
But I can not see and code compilation in QML file. and error comes:
qrc:/qt/qml/TeslaPanel/ui/RightScreen/RightScreen.qml:121: ReferenceError: systemHandler is not defined
I want to learn QML in Qt6 but this obstacles make me sad. :(
Regards,
Mucip:) -
Hi,
AFAIR, you should setup your properties before loading your qml files.
-
Hi,
I did everything according to tutorial.
He can see code compilation but I can not see.
But all codes are working as expected without any problem.
The tutorial made in Qt5 and I work now in Qt6.Regards,
Mucip:)@Mucip said in Qt 6.9 and QML harmony?:
Hi,
I did everything according to tutorial.Which tutorial?
The tutorial made in Qt5 and I work now in Qt6.
There must be a misunderstanding.
QQmlApplicationEngine::loadFromModule():This function was introduced in Qt 6.5.
-
Hi,
Tutorial is: https://www.youtube.com/watch?v=Tq-E6lqO6tMRegards,
Mucip:) -
Hi,
Tutorial is: https://www.youtube.com/watch?v=Tq-E6lqO6tMRegards,
Mucip:)@Mucip said in Qt 6.9 and QML harmony?:
Hi,
Tutorial is: https://www.youtube.com/watch?v=Tq-E6lqO6tMToo long (didn't watch) for me. As @SGaist mentions, setting up the context property should happen before attempting to load the root component. If the tutorial demonstrated loading from a remote source, it might have worked generally worked despite the race condition.
https://doc.qt.io/qt-6/qqmlapplicationengine.html#loadFromModule
If the type originates from a QML file located at a remote url, the type will be loaded asynchronously.
I interpret this as an indication that a file from a local url will be loaded synchronously.
-
Hi,
The main problem is intellisense, code compilation doesnt'work.
But program is working anyways...Is there any basic helloworld doument how to bind cpp functions to QML but in CMake version?
Regards,
Mucip:) -
Dear Sgaist
Well, Where should I make mistake than? Could you check please? Program is working but no intellisense.#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include <Controllers/system.h> #include <Controllers/hvachandler.h> int main(int argc, char *argv[]) { #if QT_VERSION < QT_VERSION_CHECK(6,0,0) QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); #endif QGuiApplication app(argc, argv); static System m_systemHandler; HVACHandler m_driverHVACHandler; HVACHandler m_passangerHVACHandler; QQmlApplicationEngine engine; QQmlContext * context(engine.rootContext()); context->setContextProperty("systemHandler", &m_systemHandler); context->setContextProperty("driverHVAC", &m_driverHVACHandler); context->setContextProperty("passangerHVAC", &m_passangerHVACHandler); QObject::connect( &engine, &QQmlApplicationEngine::objectCreationFailed, &app, []() { QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.loadFromModule("TeslaPanel", "Main"); return app.exec(); }
Reards,
Mucip:) -
Dear Sgaist
Well, Where should I make mistake than? Could you check please? Program is working but no intellisense.#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include <Controllers/system.h> #include <Controllers/hvachandler.h> int main(int argc, char *argv[]) { #if QT_VERSION < QT_VERSION_CHECK(6,0,0) QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); #endif QGuiApplication app(argc, argv); static System m_systemHandler; HVACHandler m_driverHVACHandler; HVACHandler m_passangerHVACHandler; QQmlApplicationEngine engine; QQmlContext * context(engine.rootContext()); context->setContextProperty("systemHandler", &m_systemHandler); context->setContextProperty("driverHVAC", &m_driverHVACHandler); context->setContextProperty("passangerHVAC", &m_passangerHVACHandler); QObject::connect( &engine, &QQmlApplicationEngine::objectCreationFailed, &app, []() { QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.loadFromModule("TeslaPanel", "Main"); return app.exec(); }
Reards,
Mucip:)@Mucip said in Qt 6.9 and QML harmony?:
Program is working but no intellisense.
If you use
setContextProperty()
orqmlRegister*()
functions, then the classes are only registered AFTER the program starts running. That's why code completion (Intellisense) doesn't work -- because your classes have not been registered so the QML tools can't see your classes.The documentation contains a warning about the problems of context properties: https://doc.qt.io/qt-6/qtqml-cppintegration-contextproperties.html
Instead, use the
QML_ELEMENT
+QML_SINGLETON
macro withqt_add_qml_module()
to register your class automatically when building your project. Then, code completion can work.The "QML Video" example (https://doc.qt.io/qt-6/qtmultimedia-video-qmlvideo-example.html ) shows how to do this with its VideoSingleton class:
- https://code.qt.io/cgit/qt/qtmultimedia.git/tree/examples/multimedia/video/qmlvideo/qmlvideo/videosingleton.h?h=6.9
- https://code.qt.io/cgit/qt/qtmultimedia.git/tree/examples/multimedia/video/qmlvideo/qmlvideo/CMakeLists.txt?h=6.9
For more information about integrating C++ and QML, see https://doc.qt.io/qt-6/qtqml-cppintegration-overview.html