【问题标题】:Exposing an enum in a QML signal在 QML 信号中公开枚举
【发布时间】:2015-05-19 15:06:21
【问题描述】:

我在接收信号 mySignal 时遇到问题,并且在 qml 中使用了正确的枚举类型。我认为我所做的一切都是正确的:

  • 枚举在一个单独的类中,派生自QObject
  • 枚举注册到Q_ENUMS
  • 包含枚举的类注册到qmlRegisterUncreatableType
  • 定义信号的类,也是QObject,也注册了qmlRegisterUncreatableType

值得注意的事实:

  • 运行程序会打印从 main.qml 调用的槽中的日志,但发出的信号在 qml 中没有接收到。
  • 取消注释 textclass.cpp 中的 emit 允许在 QML 中接收信号,但不会触发与枚举值的比较。

为什么我在 QML 中没有接收到枚举值向上的信号,为什么我无法与 QML 中的枚举值进行比较,即使它暴露在 Q_ENUMS 中?

我在 Qt 缺陷页面上看到了有关枚举的缺陷报告,因此我知道 Qt 中对枚举的支持并非坚如磐石。但是,我真的希望能够将值与 QML 中的枚举值进行比较,以获得接收到的信号,而目前我不是。出于某种原因,输入 mySlot 的值很乐意接受信号处理程序中无法识别的相同枚举值。下面的代码清单。

使用了 Ubuntu 上的 Qt 5.4。

错误.h:

#ifndef ERROR_H
#define ERROR_H

#include <QObject>
#include <QtQml>

class Error : public QObject
{
    Q_OBJECT
    Q_ENUMS(Type)

public:
    explicit Error(QObject *parent = 0) {Q_UNUSED(parent)};
    ~Error() {};

    static void RegisterTypes(void)
    {
        qmlRegisterUncreatableType<Error>("Types", 1, 0, "Error", "Error class uncreatable");
    }

    enum Type {
        OK = 0,
        FILE_ERROR
    };
};

#endif // ERROR_H

testclass.h:

#ifndef TESTCLASS_H
#define TESTCLASS_H

#include <QObject>
#include <QVariant>
#include <QDebug>

#include "error.h"

class TestClass : public QObject
{
    Q_OBJECT

public:
    explicit TestClass(QObject *parent = 0);
    ~TestClass();

    static void RegisterTypes(void)
    {
        qmlRegisterUncreatableType<TestClass>("TestClass", 1, 0, "TestClass", "TestClass uncreatable");
    };

signals:
    void mySignal(Error::Type arg);
    void mySignal(int arg);

public slots:
    void mySlot(QVariant arg);
};

#endif // TESTCLASS_H

testclass.cpp:

#include "testclass.h"

TestClass::TestClass(QObject *parent) : QObject(parent)
{

}

TestClass::~TestClass()
{

}

void TestClass::mySlot(QVariant arg)
{
    qDebug() << "mySlot called" << arg.toInt();
    int retval = static_cast<int>(arg.toInt());
    emit mySignal(static_cast<Error::Type>(retval));
    //   emit mySignal(retval);
}

main.cpp:

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtQml>

#include "error.h"
#include "testclass.h"

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;

    Error::RegisterTypes();
    TestClass::RegisterTypes();

    TestClass* tPtr = new TestClass();

    engine.rootContext()->setContextProperty("_t", tPtr);

    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    return app.exec();
}

main.qml:

import QtQuick 2.4
import QtQuick.Window 2.2
import Types 1.0
import TestClass 1.0

Window {
    visible: true
    width: button.width + 20
    height: button.height + 20
    Rectangle {
        id: button
        width: theText.width + 20
        height: theText.height + 20
        anchors.margins: 10
        anchors.centerIn: parent
        border.width: 3

        Connections {
            target: _t

            onMySignal: {
                console.log("onMySignal: ", arg)
                if (arg == Error.OK) {
                    console.log("OK")
                }
                else if (arg == Error.FILE_ERROR) {
                    console.log("FILE_ERROR")
                }
                else {
                    console.log("UNDEFINED")
                }
            }
        }

        Text {
            id: theText
            anchors.centerIn: parent
            text: "press me!"
            font.pixelSize: 30
        }

        MouseArea {
            anchors.fill: parent
            onClicked: _t.mySlot(Error.FILE_ERROR)
            onPressed: button.scale = 0.9
            onReleased: button.scale = 1.0
        }
    }
}

【问题讨论】:

  • 你重载了mySignal,QML 不支持它,但我在文档中找不到任何关于它的信息。
  • 是的,我知道。如果它被删除,它也不起作用。插槽中提供了 File.Error,如果我删除了 int 原型,则会将 undefined 返回给 QML。

标签: c++ qt enums qml


【解决方案1】:

这个问题有两个方面。

第一个问题:

  • Error::Type 没有在 Meta 类型系统中注册, 在Error类声明之后需要添加以下行:

    Q_DECLARE_METATYPE(错误::类型)

第二个问题:

  • 我是否为错误选择了名称“错误” 我注册的时候上课。这与内置类型发生冲突。这 注册需要改成这样:

    qmlRegisterUncreatableType("Types", 1, 0, "ErrorClass", "Error class uncreatable");

    (QML 代码也需要更新以反映这一点 当然。)

改变这两件事解决了问题,现在可以在 QML 代码中正确接收信号。

【讨论】:

  • 没有这个 Q_DECLARE_METATYPE(Error::Type) 也应该可以工作。
猜你喜欢
  • 1970-01-01
  • 2018-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-06
  • 1970-01-01
  • 2011-04-15
  • 1970-01-01
相关资源
最近更新 更多