【发布时间】: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。