热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在QQmlPropertyMap的派生类中无法调用槽函数或Q_INVOKABLE方法?

在尝试对QQmlPropertyMap类进行测试驱动开发时,发现其派生类中无法正常调用槽函数或Q_INVOKABLE方法。这可能是由于QQmlPropertyMap的内部实现机制导致的,需要进一步研究以找到解决方案。

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.

我正在测试驱动QQmlPropertyMap类。如果我能子类化它,它似乎可以很好地满足我的需要。这里的文档甚至给出了一些基本的指示,说明如何对它进行子类化。上述文档还表明这个类源自QObject。

For what it's worth, I'm using QtCreator 2.6.1 on Qt 5.0.0 with QtQuick 2.0.

不管怎样,我在Qt 5.0.0和QtQuick 2.0上使用QtCreator 2.6.1。

My main.qml:

我的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();
        }
    }
}

My main.cpp:

我的main.cpp:

#include 
#include "qtquick2applicationviewer.h"
#include "TestMap.h"
#include 

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();
}

My TestMap.h

我TestMap.h

#ifndef TESTMAP_H
#define TESTMAP_H

#include 
#include 
#include 

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

当我运行时,会有一个窗口显示“value”,正如我所期望的那样。但是当我点击窗口时,我得到一个控制台输出

TypeError: Property 'testFunc' of object TestMap(0xaaa0b8) is not a function

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_OBJECT宏的人。这一定是我在代码中做错的事情,因为如果我在TestMap文件的注释中显示了所有的更改(然后离开main)。cpp和主要。qml正好是),我得到了我期望的qDebug消息。

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.

我不确定是否应该Q_DECLARE_METATYPE(我认为2-arg受保护的构造函数应该为我这样做),但这两种方法都不起作用。

For the record, the only things I have to change to get it to work are:

就记录而言,我唯一需要改变的事情就是:

1) Derive from QObject instead of QQmlPropertyMap.

1)从QObject而不是QQmlPropertyMap派生。

2) Change the constructor to:

2)将构造函数改为:

TestMap(QObject* parent = 0): QObject(parent) {}

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?

就是这样。因为它在我不改变主线的时候起作用。cpp,主要。qml,或者插槽本身,我必须得出结论这些都没有问题。有人能告诉我我做错了什么吗?

1 个解决方案

#1


1  

This is now fixed in Qt 5.1.0 and onwards. See the following codereview url for details:

这在Qt 5.1.0中已经得到了修正。详情请参见下面的codereview url:

https://codereview.qt-project.org/#change,57418

https://codereview.qt-project.org/的变化,57418年


推荐阅读
author-avatar
刘国彬2012_380
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有