Touble calling a simple c++ function in QML
-
I am new to Qt/Qml and want to integrate C++ class into qml project. I want to use a method SquareIt() from Qml from a class Calculator. Here is the class I wrote
@#ifndef CALCULATOR_H
#define CALCULATOR_H#include <QObject>
class Calculator : public QObject
{
Q_OBJECT
public:
explicit Calculator(QObject *parent = 0);Q_INVOKABLE int SqaureIt(int a);
signals:
public slots:
};
#endif // CALCULATOR_H
@Here is how I am binding them. Note I didn't find any example which binds with QQmlApplicationEngine, is that a problem?
@#include <QGuiApplication>
#include <QQmlApplicationEngine>#include "calculator.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);QQmlApplicationEngine engine; Calculator calc; engine.setProperty("TheCalc", &calc); engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); return app.exec();
}@
I get error:
@C:\Users...\My Qt Projects\Load\main.cpp:14: error: C2248: 'QVariant::QVariant' : cannot access private member declared in class 'QVariant'@
-
Hi,
It should rather be:
@engine.rootContext()->setContextProperty("TheCalc", &calc);@
Hope it helps