简体   繁体   中英

Auto completion in QtCreator for C++ types

I define some classes in C++ to be used in QML. When coding, I would like that QtCreator automatically lists all the available properties and functions of the C++ objects.

The following example illustrates the problem: In C++ I define an Office class (with address property and a sendEmail method). I also define a Company class (with a headquarters property and a list of offices). The code of both classes is added at the end.

In main.cpp I add the following lines with the intention to make the types known to QML/javascript.

Company theCompany;
engine.rootContext()->setContextProperty("theCompany", &theCompany);
qmlRegisterType<Office>();

However, this doesn't seem to be sufficient: in the QML code below, I list which properties/methods are auto-completed and which aren't

import QtQuick 2.9
import QtQuick.Window 2.2

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    property var company: theCompany         //auto-completion OK
    property var hq: theCompany.headquarters //auto-completion OK
    property var hqAddress: hq.address       //auto-completion NOT OK
    property var offices: theCompany.offices //auto-completion OK
    property var firstOffice: offices[0]
    property var firstAddress:  firstOffice.address  //auto-completion NOT OK
    function update() {
        hq.sendEMail()                       //auto-completion NOT OK
    }
}

Hence the question is: can I make the Office type known to QML and if so, how? Will this also work for a QList<Office*> ?

Thanks in advance,

Marc

==================================

office.h

#ifndef OFFICE_H
#define OFFICE_H

#include <QObject>

class Office : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString address MEMBER m_address NOTIFY addressChanged)
public:
    explicit Office(QObject *parent = nullptr);
    Q_INVOKABLE void sendEMail(QString message){};

signals:
    void addressChanged();

public slots:

private:
    QString m_address;
};

#endif // OFFICE_H

office.cpp

#include "office.h"

Office::Office(QObject *parent) : QObject(parent)
{

}

company.h

#ifndef COMPANY_H
#define COMPANY_H

#include <QObject>

#include "office.h"

class Company : public QObject
{
    Q_OBJECT
    Q_PROPERTY(Office* headquarters MEMBER m_headquarters NOTIFY headquartersChanged)
    Q_PROPERTY(QList<Office *> offices MEMBER m_offices NOTIFY officesChanged)
public:
    explicit Company(QObject *parent = nullptr);

signals:
    void officesChanged();
    void headquartersChanged();

public slots:
private:
    QList<Office*> m_offices;
    Office* m_headquarters;
};

#endif // COMPANY_H

company.cpp

#include "company.h"

Company::Company(QObject *parent) : QObject(parent)
{

}

main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>

#include "company.h"

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;
    Company theCompany;
    engine.rootContext()->setContextProperty("theCompany", &theCompany);
    qmlRegisterType<Office*>();
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;
    return app.exec();
}

I see:

property var company: theCompany         //auto-completion OK
property var hq: theCompany.headquarters //auto-completion OK
property var hqAddress: hq.address       //auto-completion NOT OK
property var offices: theCompany.offices //auto-completion OK
property var firstOffice: offices[0]
property var firstAddress:  firstOffice.address  //auto-completion NOT OK
function update() {
    hq.sendEMail()                       //auto-completion NOT OK
}

There I never see a

property Office firstOffice

I see

property var firstOffice

The type var has no property address , so there is no code completion.
The property only exists at runtime.
At least for me, when Office is registered (with a name) and the type is Office , QtCreator will give me proper code completion.

Same goes for hq - not even as reader of that QML file I have any idea, what type it might be. Why should the QtCreator know this?


BTW: Similar to the QtCreator, also the JIT has no knowledge on the properties, so with a type var in the equation, there won't be any optimized bindings .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM