Qt QML 单例智能感知

Qt QML Singleton Intellisense

Qt QML,它们确实有效,但在 QML 参考中没有智能感知。我可以实现智能感知吗?

pragma Singleton
import QtQuick 2.0
import QtQuick.Window 2.3

Item {
    property int iMode: 0
    property bool bSwapLeftRight: false

正在注册:

qmlRegisterSingletonType(QUrl(QLatin1String("qrc:/GlobalSettings.qml")), "ncs.global", 1, 0, "SingletonSettings" );

第二个例子;:

ctxt->setContextProperty("someModel", QVariant::fromValue(ownModel));
ctxt->setContextProperty("globalControl", QVariant::fromValue(globalControl.Get()));

第一个我在QML中使用时实现了IntelliSense,第二个是单例,我没有实现IntelliSense。计划通过这个将一些 c++ 定义的枚举公开给 QML,但是当没有提供 IntelliSense 时失去价值..

问题基本上是,Qt Quick 和 Creator 是否支持单例的 IntelliSense 类?值得进一步调查吗?

添加更多细节,主要问题不是关于枚举,而是 IntelliSense(自动完成):

A few snippets:

main.cpp:
    //Regular Pointer
        SomeModel* ownModel = new SomeModel();
    //Custom Singleton implementation
        GlobalControlSP globalControl = GlobalControl::GetInstance();

    //I did not really want this line. GlobalControl should be singleton, how would this be threated?
        qmlRegisterType<GlobalControl>("ncs.global", 1, 0, "Global");

    //Registering a "Pragma Singleton" file, Intellisense do not work
        qmlRegisterSingletonType(QUrl(QLatin1String("qrc:/GlobalSettings.qml")), "ncs.global", 1, 0, "SingletonSettings" );

        QQmlApplicationEngine engine;
        QQmlContext* ctxt = engine.rootContext();

    //Regular context property, not singleton. Intellisense works
        ctxt->setContextProperty("ownModel", QVariant::fromValue(ownModel));

    //Registering the singleton as context property, Intellisense do not work
        ctxt->setContextProperty("globalControl", QVariant::fromValue(globalControl.Get()));

        engine.load(QUrl(QLatin1String("qrc:/main.qml")));

globalcontrol.h:
class GlobalControl : public QObject,
{
    Q_OBJECT
    Q_PROPERTY(QString backcolor READ backcolor NOTIFY backcolorChanged)
    ....

public:
    GlobalControl(QObject *parent = nullptr);
    QString backcolor() const { return m_backColor; }
    ....
    enum EnButton
    {
        DEVICE_START,
        DEVICE_IR,
        .....
    };
    Q_ENUM(EnButton)

public slots:
    void changeMode(int mode);
    void buttonPressed(int button);


some qml file:

//This implementation works:
    NcsButton {
        property int valuent
        id:ir
        text: qsTr("IR")
        width: 66 * widthScaling
        Layout.row: 4
        Layout.column: 1
        fontColor: bIr ? valueColor : textColor
        onClicked: {
            globalControl.buttonPressed(Global.DEVICE_IR)

//This does not work:
    NcsButton {
        id:ir
        text: qsTr("IR")
        width: 66 * widthScaling
        Layout.row: 4
        Layout.column: 1
        fontColor: bIr ? valueColor : textColor
        onClicked: {
            globalControl.buttonPressed(globalControl.DEVICE_IR)

解决方案或解决方法:

对于 QML 定义的单例,将生成的文件添加到 Qt 编译器 QML 库

..\..\..\bin\qmlplugindump -relocatable ncs.global 1.0 > plugins.qmltypes

对于c++注册的单例,制作一个"fake"普通class类型的指针。登记。要从同一个单例 class 中检索枚举,请创建一个 UncreatableType:

    GlobalControlSP globalControlOrig = GlobalControl::GetInstance();
    GlobalControl* globalControl = globalControlOrig.Get();
    ctxt->setContextProperty("globalControl", QVariant::fromValue(globalControl));
    qmlRegisterUncreatableType<GlobalControl>("ncs.global", 1, 0, "Global", "Singleton");