Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Autocompletion Not Working in QML for C++ Instances
Forum Updated to NodeBB v4.3 + New Features

Autocompletion Not Working in QML for C++ Instances

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 5 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    learis
    wrote on 1 Oct 2022, 15:20 last edited by learis 10 Jan 2022, 15:50
    #1

    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

    1 Reply Last reply
    0
    • V Offline
      V Offline
      Vichente1
      wrote on 4 Oct 2022, 07:00 last edited by
      #2

      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.

      1 Reply Last reply
      0
      • R Offline
        R Offline
        RangerQT
        wrote on 6 Oct 2022, 04:26 last edited by RangerQT 10 Jun 2022, 05:00
        #3

        Thanks. I will try this as I have been having this issue.

        Where do I change these. I am on 6.4.x

        J 1 Reply Last reply 6 Oct 2022, 05:08
        0
        • R RangerQT
          6 Oct 2022, 04:26

          Thanks. I will try this as I have been having this issue.

          Where do I change these. I am on 6.4.x

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 6 Oct 2022, 05:08 last edited by
          #4

          @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...".

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • G Offline
            G Offline
            GrecKo
            Qt Champions 2018
            wrote on 6 Oct 2022, 09:05 last edited by
            #5

            Don't use setContextProperty to expose your C++ instances.

            https://doc.qt.io/qt-6/qtqml-cppintegration-overview.html#choosing-the-correct-integration-method-between-c-and-qml

            R 1 Reply Last reply 6 Oct 2022, 18:36
            1
            • G GrecKo
              6 Oct 2022, 09:05

              Don't use setContextProperty to expose your C++ instances.

              https://doc.qt.io/qt-6/qtqml-cppintegration-overview.html#choosing-the-correct-integration-method-between-c-and-qml

              R Offline
              R Offline
              RangerQT
              wrote on 6 Oct 2022, 18:36 last edited by
              #6

              @GrecKo said in Autocompletion Not Working in QML for C++ Instances:

              Don't use setContextProperty to expose your C++ instances.

              https://doc.qt.io/qt-6/qtqml-cppintegration-overview.html#choosing-the-correct-integration-method-between-c-and-qml

              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 ..
              )
              
              
              1 Reply Last reply
              0

              1/6

              1 Oct 2022, 15:20

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved