无法在 QQmlPropertyMap 的子类中从 QML 调用 slot 或 Q_INVOKABLE [英] Can't call slot or Q_INVOKABLE from QML in subclass of QQmlPropertyMap

查看:154
本文介绍了无法在 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 和 QtQuick 2.0 上使用 QtCreator 2.6.1.

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 及更高版本中修复.有关详细信息,请参阅以下代码审查网址:

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

这篇关于无法在 QQmlPropertyMap 的子类中从 QML 调用 slot 或 Q_INVOKABLE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆