Autocompletion Not Working in QML for C++ Instances
-
I have followed instructions to set a C++ class instance as a context property in order for QML to access it. I can successfully access and communicate with the C++ class instance in QML, but it's still not giving me autocomplete when I begin typing the instance. Has anyone run into this and know a solution? I already tried disabling ClangCodeModel since I read that somewhere else, but it doesn't do anything. What else can I try?
In main.cpp
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include "testerino.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); testerino testInstance; QQmlApplicationEngine engine; engine.rootContext() -> setContextProperty("testInstance", &testInstance); const QUrl url(QStringLiteral("qrc:/main.qml")); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.load(url); return app.exec(); }
In main.qml
Window { width: 640 height: 480 visible: true title: qsTr("Hello World") Button { onClicked: { testInstance.testSlot() } } }
testerino.h
#ifndef TESTERINO_H #define TESTERINO_H #include <QObject> class testerino : public QObject { Q_OBJECT public: explicit testerino(QObject *parent = nullptr); signals: public slots: void testSlot(); }; #endif // TESTERINO_H
testerino.cpp
#include "testerino.h" #include <QDebug> testerino::testerino(QObject *parent) : QObject{parent} { } void testerino::testSlot() { qDebug() << "made it"; }
Edit: I am using Qt Creator 8.0.1 Community Edition
-
I had the same issue. I fixed it disabling ClangCodeModel, ClassView, CppEditor, after Qt restarted. I activated again ClassView, CppEditor and some other plugins that automatic disabled like Divice Support plugins and Qt Quick qml plugins. If doesn't work, you need an extra step, check Generic Highlighter in Edit Prefrences -> Text Editor and Download or Reload.
-
@RangerQT said in Autocompletion Not Working in QML for C++ Instances:
Where do I change these. I am on 6.4.x
If you mean how to enable/disable plug-ins then in QtCreator (has nothing to do with Qt) go to "Help/About Plugins...".
-
Don't use
setContextProperty
to expose your C++ instances. -
@GrecKo said in Autocompletion Not Working in QML for C++ Instances:
Don't use
setContextProperty
to expose your C++ instances.Okay. Thank you. I've been through those several times and am still rereading and rereading them trying to get a complete understanding of them. I notice they use a pro file to add the components so I'll have to convert that to cmake terms.
I'm new to using Qt6 and the last time I used Qt5 was a few years ago so a lot has changed mainly the use of cmake instead of a pro file which is fine with me and I want to learn to use the cmake build process.
I've been trying to find the "right way" to do this but there seems to be a lot of conflicting posts out there and unfortunately I think many still fall back on the Qt5 processes which may work but don't help someone trying to find the Qt6 way! I use the QObject and QML_ELEMENT and QML_PROPERTY in my classes as well as including the qml registration header. So any help is appreciated.
I have a CMakeLists.txt in my project main which adds a subdirectory for utils in which I have a CMakeLists.txt that creates a static library and adds a qml module which is then linked in the main CMakeList.txt All of this is based on the blog post for Qt6 about the new modules . I assume I still need to include the Q_IMPORT_QML_PLUGIN(utilsPl,ugin) in main.cpp. Is this correct?
Then if I am correct I can use the import utils (the module URI) in my QML modules?
The Qt documentation is much better then most for sure but sometimes I feel since they know the process they use partial examples and leave out what is obvious to them. I'd love a blog on how to create a project from scratch with subdirectories, adding the libraries, and then integrating a sample C++ property and function into QML and include all the steps to do setup in main.cpp and any QML modules and of course a sample going the other way!f
utils/CMakeLists.txt
# CMakeLists.txt to build import backend. qt_add_library(utils_module STATIC) qt6_add_qml_module(utils_module URI utils VERSION 1.0 SOURCES utils.h utils.cpp )
Main CMakeLists.txt
cmake_minimum_required(VERSION 3.16) project(FlightLog VERSION 0.1 LANGUAGES CXX) set(CMAKE_AUTOMOC ON) set(CMAKE_CXX_STANDARD_REQUIRED ON) # Set application name for use in building. set(appName "FlightLogbook") #find_package(Qt6 COMPONENTS Quick REQUIRED) # Core is probably redundant due to Quick find find_package(Qt6 REQUIRED COMPONENTS Core Qml Quick Sql ) # Standard project setup #qt_standard_project_setup() # The executable qt_add_executable(FlightLogbook main.cpp ) #============================================================================== # Our subdirectories add_subdirectory( importdata ) add_subdirectory( fldbase ) add_subdirectory( utils ) #============================================================================== # QML and other files qt_add_qml_module(FlightLogbook URI FlightLog VERSION 1.0 QML_FILES main.qml Initapp.qml ) set_target_properties(FlightLogbook PROPERTIES MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} MACOSX_BUNDLE TRUE WIN32_EXECUTABLE TRUE ) # Added Core, Qml as private libraries target_link_libraries(FlightLogbook PRIVATE Qt6::Core PRIVATE Qt6::Quick PRIVATE Qt6::Qml PRIVATE Qt6::Sql ) #============================================================================== # Our libraries target_link_libraries( FlightLogbook PRIVATE import_moduleplugin PRIVATE fldbase_moduleplugin PRIVATE utils_moduleplugin ) # Attempting to put the runtime result in the directory above the build install(TARGETS FlightLogbook BUNDLE DESTINATION . LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION .. )