我正在尝试将我的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 回答
你的Q_INVOKABLE函数需要在包装器对象中公开,我希望你知道如果没有设置为public,它们是私有的 .
尝试将它们切换为公共重试 .
解决方案是将Q_INVOKABLE更改为Q_PROPERTY,在公共中定义函数,然后在我的qml中调用它们 . 所有函数似乎都正常工作并返回正确的值 .