无法从 QQmlPropertyMap 的子类中的 QML 调用 slot 或 Q_INVOKABLE
我正在尝试测试 QQmlPropertyMap
类.如果我可以对它进行子类化,它似乎可以很好地满足我的需求.here 文档甚至提供了一些关于如何处理的基本说明子类化它.所述文档还表明该类派生自 QObject
.
I'm trying to test drive the QQmlPropertyMap
class. It seems like it might work well for what I want, if I can subclass it. The documentation here even gives some rudimentary instructions on what to do for subclassing it. Said documentation also indicates that this class derives from QObject
.
不管怎样,我在 Qt 5.0.0 上使用 QtCreator 2.6.1 和 QtQuick 2.0.
For what it's worth, I'm using QtCreator 2.6.1 on Qt 5.0.0 with QtQuick 2.0.
我的 main.qml:
My main.qml:
import QtQuick 2.0
Rectangle {
width: 360
height: 360
Text {
text: owner.field
anchors.centerIn: parent
}
MouseArea {
anchors.fill: parent
onClicked: {
owner.testFunc();
}
}
}
我的 main.cpp:
My main.cpp:
#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
#include "TestMap.h"
#include <QQmlContext>
int main(int argc, char *argv[])
{
TestMap* map = new TestMap();
QGuiApplication app(argc, argv);
QtQuick2ApplicationViewer viewer;
QQmlContext* ctxt = viewer.rootContext();
ctxt->setContextProperty("owner", map);
viewer.setMainQmlFile(QStringLiteral("qml/TestMap/main.qml"));
viewer.showExpanded();
return app.exec();
}
我的 TestMap.h
My TestMap.h
#ifndef TESTMAP_H
#define TESTMAP_H
#include <QObject>
#include <QQmlPropertyMap>
#include <QDebug>
class TestMap: public QQmlPropertyMap // QObject
{
Q_OBJECT
public:
TestMap(QObject* parent = 0): QQmlPropertyMap(this, parent) // QObject(parent)
{
insert("field", "value"); // Comment this out
}
TestMap(const TestMap& value) { }
virtual ~TestMap() {}
public slots:
void testFunc()
{
qDebug() << "Success!";
}
};
Q_DECLARE_METATYPE(TestMap)
#endif
当我运行时,我得到一个窗口,上面写着价值",正如我所期望的那样.但是当我点击窗口时,我得到一个控制台输出说
When I run, I get a window saying "value", as I'd expect. But when I click on the window, I get a console output saying
TypeError: Property 'testFunc' of object TestMap(0xaaa0b8) is not a function
我寻找过类似的问题,但所有搜索结果都是关于忘记包含 Q_OBJECT
宏的人.这一定是我在代码中做错了什么,因为如果我对 TestMap 文件的注释中指示的所有更改进行了更改(并将 main.cpp 和 main.qml 保持原样),我会得到 qDebug
我期待的消息.
I've looked for similar problems, but all the search results are about people that forgot to include the Q_OBJECT
macro. It must be something I'm doing wrong in the code, because if I make all the changes indicated in the comments of the TestMap file (and leave the main.cpp and main.qml exactly as is), I get the qDebug
message I expect.
我不确定我是否应该 Q_DECLARE_METATYPE
(我认为 2-arg 受保护的构造函数应该为我做),但无论哪种方式都不起作用.
I'm not sure whether I'm supposed to Q_DECLARE_METATYPE
or not (I think the 2-arg protected constructor is supposed to do it for me), but it doesn't work either way.
为了记录,我唯一需要改变才能让它工作的是:
For the record, the only things I have to change to get it to work are:
1) 派生自 QObject
而不是 QQmlPropertyMap
.
1) Derive from QObject
instead of QQmlPropertyMap
.
2) 将构造函数改为:
2) Change the constructor to:
TestMap(QObject* parent = 0): QObject(parent) {}
就是这样.由于它在我不更改 main.cpp、main.qml 或插槽本身的任何内容时有效,因此我必须得出结论,这些都没有问题.谁能告诉我我做错了什么?
And that's it. Since it works when I don't change anything about the main.cpp, main.qml, or the slot itself, I have to conclude it's nothing wrong with those. Can anyone tell me what I'm doing wrong?
推荐答案
现在在 Qt 5.1.0 及更高版本中已修复此问题.详情见以下codereview url:
This is now fixed in Qt 5.1.0 and onwards. See the following codereview url for details:
https://codereview.qt-project.org/#change,57418
相关文章