c++ - QtCreator 中针对 C++ 类型的自动补全

标签 c++ qt autocomplete qml qt-creator

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

下面的例子说明了这个问题: 在 C++ 中,我定义了一个 Office 类(具有地址属性和一个 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
    }
}

因此问题是:我可以制作 Office 吗? QML 已知的类型,如果是,如何?这是否也适用于 QList<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

办公室.cpp

#include "office.h"

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

}

公司.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

公司.cpp

#include "company.h"

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

}

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

最佳答案

我明白了:

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。 .

关于c++ - QtCreator 中针对 C++ 类型的自动补全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51713145/

相关文章:

c++ - C/C++ - HWInfo - libhd - 如何获取所有可用设备的名称列表?

c++ - 我需要从用户输入中找到某些数字的位置

c++ - 整数初始化与使用指针初始化字符串,C++

ios - qml插件中的SensorGesture无法正常工作

c++ - 即使选择了调试构建,Qt Creator 的智能感知也会使#ifdef _DEBUG block 变灰

ajax - 使用 Symfony2 自动完成

c++ - c++ - 如何按降序打印字符串中每个字母的频率?

qt - 如何在QTreeView中添加不同类型的委托(delegate)

php - 显示数据库表中的两个字段以填充 jquery 自动完成

jquery - 将事件处理程序 Hook 到 jQuery 自动完成组合框