在QtCreator中自动完成C ++类型

时间:2018-08-06 17:53:06

标签: c++ qt autocomplete qml qt-creator

我在C ++中定义了一些要在QML中使用的类。编码时,我希望QtCreator自动列出C ++对象的所有可用属性和功能。

以下示例说明了该问题: 在C ++中,我定义了一个Office类(具有address属性和sendEmail方法)。我还定义了一个Company类(具有总部财产和办公室列表)。这两个类的代码都添加在末尾。

main.cpp中,我添加了以下几行,以使QML / javascript知道这些类型。

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

但是,这似乎还不够:在下面的QML代码中,我列出了哪些属性/方法是自动完成的,哪些不是自动完成的

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
    }
}

因此,问题是:我可以使QML知道Office类型吗?如果可以,怎么办?这对QList<Office*>也有用吗?

预先感谢

马克

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

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();
}

1 个答案:

答案 0 :(得分:1)

我知道:

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
}

我从没见过

property Office firstOffice

我知道

property var firstOffice

类型var没有属性address,因此没有代码完成。
该属性仅在运行时存在。
至少对我来说,当Office被注册(名称)并且类型为Office时,QtCreator会给我正确的代码补全。

hq也是如此-甚至作为那个QML文件的读者,我也不知道它可能是什么类型。为什么QtCreator应该知道这一点?


顺便说一句:与QtCreator相似,JIT也不了解属性,因此方程式中的类型为var,就不会有optimized bindings