Qt QML Singleton Intellisense

时间:2017-11-14 14:25:39

标签: c++ qt singleton qml 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,第二个是单身,我没有实现智能感知。计划通过这个枚举将一些c ++定义的枚举暴露给QML,但是当没有提供IntelliSense时会丢失值..

问题基本上是,Qt Quick和Creator是否支持单一类的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)

1 个答案:

答案 0 :(得分:0)

解决方案或解决方法:

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

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

对于c ++注册的单身人士,制作一个&#34;假的&#34;普通类类型的指针。寄存器。要从同一个单例类中检索枚举,请创建一个UncreatableType:

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