QML无法识别C ++函数

时间:2015-06-02 10:54:16

标签: c++ qml integration

我正在尝试将我的c ++类实现为QML,通过设置context属性可以识别该类,我可以成功调用该类并查看所有函数,但在运行时它们无法识别并返回错误: TypeError:Property' getSrcImage'对象包装器(0x7b211cbf10)不是一个函数,我相信函数没有正确地声明为QML但不知道如何解决..

.h文件

class Wrapper : public QObject
{
    Q_OBJECT
    Q_INVOKABLE void initiateLipLib();
    Q_INVOKABLE bool setMat();
    Q_INVOKABLE QImage displayfeed();
    Q_INVOKABLE void getMatFeed();
    Q_INVOKABLE int liptrainstart(cv::Mat Image);
    Q_INVOKABLE void liptrainingend();
    Q_INVOKABLE float getDistance();
    Q_INVOKABLE std::string getstatus();
    Q_INVOKABLE void clear();
public:
    explicit Wrapper(QObject *parent = 0);
    QString getSrcImage();

的main.cpp

#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQuickView>
#include <QtQml/QQmlContext>
#include <QDebug>
#include "wrapper.h"



int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("wrapper", new Wrapper);
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    return app.exec();
}

.qml

Image {
            id: camfeed
            visible: false
            source: wrapper.getstatus()
            anchors.centerIn: camcontainer
}

2 个答案:

答案 0 :(得分:3)

你的Q_INVOKABLE函数需要在你的包装器对象中公开,我希望你知道如果没有设置为public,它们是私有的。

尝试将它们切换为公开重试。

class Wrapper : public QObject
{
    Q_OBJECT
public:
    Q_INVOKABLE void initiateLipLib();
    Q_INVOKABLE bool setMat();
    Q_INVOKABLE QImage displayfeed();
    Q_INVOKABLE void getMatFeed();
    Q_INVOKABLE int liptrainstart(cv::Mat Image);
    Q_INVOKABLE void liptrainingend();
    Q_INVOKABLE float getDistance();
    Q_INVOKABLE std::string getstatus();
    Q_INVOKABLE void clear();

    explicit Wrapper(QObject *parent = 0);
    QString getSrcImage();

答案 1 :(得分:-1)

解决方案是将Q_INVOKABLE更改为Q_PROPERTY,在公共中定义函数,然后在我的qml中调用它们。所有函数似乎都正常工作并返回正确的值。