Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to fix QML code editor auto-completion for integrated C++ methods?



  • Hello, All!

    In my QTCreator for QML code auto-completion for integrated C++ methods doesn't work.

    For instance, I did Myclass class and pass object pointer to QML engine by setContextProperty method.

    QQmlApplicationEngine engine;
    Myclass* confptr = new Myclass();
    engine.rootContext()->setContextProperty("testCPP", confptr);
    

    In QML code after I input ...

    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
    
        Column {
            spacing: 10
    
            Button {
                onClicked: testCPP.
    

    ... there is no auto-completion popup. So I need copy method name manually from C++ code.

    After running application it works properly but manual inputs disturbs me.

    Can you give an advice, how I can solve this problem?

    Myclass.h

    #ifndef MYCLASS_H
    #define MYCLASS_H
    
    #include <QObject>
    
    class Myclass : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(int engineRPM READ engineRPM WRITE setEngineRPM NOTIFY engineRPMChanged)
    public:
        Myclass();
        int engineRPM() const;
    
    signals:
        void engineRPMChanged();
    
    private:
        int m_engineRPM;
    
    public slots:
        void setEngineRPM (int temp);
    
    };
    
    
    
    #endif // MYCLASS_H
    

    Myclass.cpp

    #include "myclass.h"
    #include <QDebug>
    
    Myclass::Myclass()
    {
        setEngineRPM(100);
    }
    
    void Myclass::setEngineRPM (int temp){
    
        m_engineRPM = temp;
        qDebug () << "Some text here" << temp;
    }
    
    int Myclass::engineRPM() const{
    
        return m_engineRPM;
    
    }
    

    Main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    
    #include "myclass.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);
    
        QQmlApplicationEngine engine;
    
        Myclass* confptr = new Myclass();
        confptr->setEngineRPM(1);
    
        engine.rootContext()->setContextProperty("testCPP", confptr);
    
        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();
    }
    

    main.qml

    import QtQuick 2.15
    import QtQuick.Window 2.15
    import QtQuick.Controls 2.12
    
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
    
        Column {
            spacing: 10
    
            Button {
                onClicked: testCPP.setEngineRPM(1000)
            }
    
            Text {
                text: testCPP.engineRPM
            }
        }
    }
    

    I have installed:

    Qt Creator 5.0.0 Based on Qt 5.15.2 (MSVC 2019, 64 bit)

    OS: Windows 10 21H1 build 19043.1415


  • Moderators



  • @raven-worx , is there a way to use setContextProperty with auto-completion feature?