Jos*_*all 3 c++ integration qml
我正在尝试将我的 C++ 类实现到 QML 中,通过设置上下文属性来识别该类,我可以成功调用该类并查看所有函数,但在运行时无法识别它们并返回错误:TypeError: Property 'getSrcImage' object Wrapper(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();
Run Code Online (Sandbox Code Playgroud)
主程序
#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();
}
Run Code Online (Sandbox Code Playgroud)
.qml
Image {
id: camfeed
visible: false
source: wrapper.getstatus()
anchors.centerIn: camcontainer
}
Run Code Online (Sandbox Code Playgroud)
您的 Q_INVOKABLE 函数需要public在您的包装器对象内,我希望您知道如果未设置为public,它们就是private.
尝试将它们切换为公开重试。
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();
Run Code Online (Sandbox Code Playgroud)