C++ 在QML信号中公开枚举
我在接收qml中具有正确枚举类型的信号时遇到问题。我认为我做的每件事都是对的:C++ 在QML信号中公开枚举,c++,qt,enums,qml,C++,Qt,Enums,Qml,我在接收qml中具有正确枚举类型的信号时遇到问题。我认为我做的每件事都是对的: 枚举位于单独的类中,派生自QObject 枚举已注册到Q_ENUMS 包含枚举的类已注册到qmlregisteruncreateabletype 定义信号的类也是一个QObject,并且还注册了qmlRegisterUncreatableType 值得注意的事实: 运行该程序会在从main.qml调用的插槽中打印日志,但在qml中不会接收到发出的信号 在textclass.cpp中取消对emit的注释允许在QM
- 枚举位于单独的类中,派生自
QObject
- 枚举已注册到
Q_ENUMS
- 包含枚举的类已注册到
qmlregisteruncreateabletype
- 定义信号的类也是一个
,并且还注册了QObject
qmlRegisterUncreatableType
- 运行该程序会在从main.qml调用的插槽中打印日志,但在qml中不会接收到发出的信号
- 在textclass.cpp中取消对emit的注释允许在QML中接收信号,但不会触发与枚举值的比较
#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
这方面的问题是双重的 第一个问题:
- 错误::类型未在元类型系统中注册,则 需要在错误类声明后添加以下行: Q_DECLARE_元类型(错误::类型)
- 是我为错误选择了名称“Error” 当我注册的时候。这与内置类型冲突。这个 注册需要更改为: qmlRegisterUncreatableType(“类型”,1,0,“错误类”,“错误类不可处理”) (QML代码也需要更新以反映这一点。) 当然可以。)
更改这两件事解决了问题,现在可以在QML代码中正确接收信号。您重载了QML不支持的
mySignal
,但我在文档中找不到任何关于它的信息。是的,我知道。但是,如果将其删除,它也不起作用。插槽中提供了File.Error,如果我删除int原型,undefined将返回给QML。如果没有这个Q_DECLARE_元类型(Error::Type),它也应该可以工作。
#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
#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);
}
#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();
}
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
}
}
}