QMLLS not finding manual C++ registrations
-
The issue
I have a very basic Qt Quick App which builds and runs without issues. However importing QML modules which are manually first registered at runtime by my C++ code in
main.cpp
are obviously not identified by the QMLLS and therefore I get false warnings for importing the module and whenever I access something from it. This makes QML development with C++ classes/instances registered at runtime quite painful since I don't get auto-completion in the editor.Here is Main.qml and the project structure on the left:
CMakeLists.txt:
cmake_minimum_required(VERSION 3.16) project(SongPlayer VERSION 0.1 LANGUAGES CXX) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(QT_QML_GENERATE_QMLLS_INI ON) find_package(Qt6 6.5 REQUIRED COMPONENTS Quick) qt_standard_project_setup(REQUIRES 6.5) qt_add_executable(appSongPlayer main.cpp ) qt_add_qml_module(appSongPlayer URI SongPlayer VERSION 1.0 QML_FILES Main.qml SOURCES TestClass.h TestClass.cpp NO_CACHEGEN ) # Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1. # If you are developing for iOS or macOS you should consider setting an # explicit, fixed bundle identifier manually though. set_target_properties(appSongPlayer PROPERTIES # MACOSX_BUNDLE_GUI_IDENTIFIER com.example.appSongPlayer MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} MACOSX_BUNDLE TRUE WIN32_EXECUTABLE TRUE ) target_link_libraries(appSongPlayer PRIVATE Qt6::Quick ) include(GNUInstallDirs) install(TARGETS appSongPlayer BUNDLE DESTINATION . LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} )
main.cpp:
#include <QGuiApplication> #include <QQmlApplicationEngine> #include "TestClass.h" int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; qmlRegisterSingletonInstance("MyTestModule", 1, 0, "TestInstance", new TestClass(&app)); QObject::connect( &engine, &QQmlApplicationEngine::objectCreationFailed, &app, []() { QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.loadFromModule("SongPlayer", "Main"); return app.exec(); }
TestClass.h:
#ifndef TESTCLASS_H #define TESTCLASS_H #include <QObject> #include <qqml.h> using namespace Qt::Literals::StringLiterals; class TestClass : public QObject { Q_OBJECT Q_PROPERTY(QString myText READ getMyText NOTIFY myTextChanged) public: explicit TestClass(QObject *parent = nullptr); QString getMyText() const { return u"Hello QMLSS"_s; } signals: void myTextChanged(); private: }; #endif // TESTCLASS_H
Proof those are false warnings and the app builds:
Question
What's the best way to inform the QMLLS of my module (and classes in it) if it's only registered from C++ code as seen here with
qmlRegisterSingletonInstance
(or in theory withQQmlContext::setContextProperty
) for smooth QML development in Qt Creator?Environment
- Windows 10 64-bit
- Qt Creator 14.0.2 (Community)
- Qt 6.8.0
- Project: Qt Quick Application, Kit: Destkop Qt 6.8.0 MinGW 64-bit with CMake
-
instead of setContextProperty() you should use QML_SINGLETON
see: https://t1p.de/ekkeQML_SINGLETON -
Hello again :-)
Check this documentation, please.
TheQML_ELEMENT
macro is missing inTestClass
. -
@Axel-Spoerl Hi again too. I attempted to add
QML_ELEMENT
previously and that didn't help but to make sure I did not mess something up I just tried again. Here is also proof that it's not some kind of weird cache or that it gets fixed after building:
https://streamable.com/61he1v -
-
instead of setContextProperty() you should use QML_SINGLETON
see: https://t1p.de/ekkeQML_SINGLETON -
-