Josh Marshall
Josh Marshall

Reputation: 159

QML is not recognising C++ functions

I am trying to implement my c++ class into QML, the class is recognised by setting the context property and I can successfully call the class and see all the functions but on runtime they are not recognised and returns the error: TypeError: Property 'getSrcImage' of object Wrapper(0x7b211cbf10) is not a function, I believe that the functions are not getting properly declared to the QML but do not know how to fix..

.h file

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
}

Upvotes: 3

Views: 3398

Answers (2)

MokaT
MokaT

Reputation: 1416

Your Q_INVOKABLE functions needs to be public inside your wrapper object, and I hope you know that if not set to public, they are private.

Try switching them to public an retry.

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

Upvotes: 7

Josh Marshall
Josh Marshall

Reputation: 159

The solution was to change the Q_INVOKABLE to Q_PROPERTY, defined the functions in public then called them within my qml. All functions seem to be working correctly and returning the correct values.

Upvotes: -1

Related Questions