[Solved] error while Embedding C++ object into qml components
-
I wanted to check out how well the embedding of c++ objects in qml works
For this purpose I have the following code in QT Quick Applicationmain.cpp
@#include <QtGui/QApplication>
#include <QDeclarativeContext>
#include <QKeyEvent>
#include "qmlapplicationviewer.h"class Factorial : public QObject {
int x;public:
Factorial(int z) {
x=z;
}
Q_INVOKABLE QString fact() const {
int f=1;
QString str;
for(int i=x;i>0;i--)
f=f*i--;
str=QString::number(f);
return str;
}
};int main(int argc, char *argv[])
{
QApplication app(argc, argv);QmlApplicationViewer viewer; Factorial f(10); viewer.rootContext()->setContextProperty("factorial",&f); viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto); viewer.setMainQmlFile(QLatin1String("qml/test1/main.qml")); viewer.showExpanded(); return app.exec();
}
@main.qml:
@import QtQuick 1.0Rectangle {
id: rectangle1
width: 360
height: 360Rectangle { id: rectangle2 x: 93 y: 89 width: 157 height: 80 color: "#912323" MouseArea { id:marea1 anchors.fill: parent onClicked: console.log(factorial.fact()) } }
}
@Now when I run the code and click the mouse area I get the following error message
file:///home/sinjar/nokia/test1-build-simulator/qml/test1/main.qml:18: TypeError: Result of expression 'factorial.fact' [undefined] is not a function.Can someone please tell me where i am going wrong
Thanks