Access C++ function from QML
For any C++ code to be called from QML, it must reside inside a QObject
.
What you need to do is create a QObject
descended class with your function, register it to QML, instantiate it in your QML and call the function.
Note also that you have to mark your function with Q_INVOKABLE
.
Code:
#ifndef EIGEN_FUNCTION_HEADER_H
#define EIGEN_FUNCTION_HEADER_H
#include <QObject>
class MyObject : public QObject{
Q_OBJECT
public:
explicit MyObject (QObject* parent = 0) : QObject(parent) {}
Q_INVOKABLE int reken_tijden_uit(){
return 1;
}
};
#endif // EIGEN_FUNCTION_HEADER_H
main.cpp:
#include <QtGui/QApplication>
#include <QtDeclarative>
#include "qmlapplicationviewer.h"
#include "eigen_function_header.h"
Q_DECL_EXPORT int main(int argc, char *argv[])
{
QScopedPointer<QApplication> app(createApplication(argc, argv));
qmlRegisterType<MyObject>("com.myself", 1, 0, "MyObject");
QmlApplicationViewer viewer;
viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
viewer.setMainQmlFile(QLatin1String("qml/tw_looptijden_berekenen/main.qml"));
viewer.showExpanded();
return app->exec();
}
QML:
import QtQuick 1.1
import com.myself 1.0
Rectangle {
width: 360
height: 360
Text {
text: qsTr("Hello World")
anchors.centerIn: parent
}
MyObject {
id: myobject
}
MouseArea {
anchors.fill: parent
onClicked: {
console.log(myobject.reken_tijden_uit())
}
}
}
As an alternative to qmlRegisterType() in main.cpp, you can also use context properties to make QObject variables available in QML. (In case you don't require to create different instances of your object with later QML).
Q_DECL_EXPORT int main(int argc, char *argv[])
{
QScopedPointer<QApplication> app(createApplication(argc, argv));
QmlApplicationViewer viewer;
viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
viewer.setMainQmlFile(QLatin1String("qml/tw_looptijden_berekenen/main.qml"));
viewer.showExpanded();
// add single instance of your object to the QML context as a property
// the object will be available in QML with name "myObject"
MyObject* myObject = new MyObject();
viewer.engine()->rootContext()->setContextProperty("myObject", myObject);
return app->exec();
}
In QML, you can then access the object from anywhere in your code with the given name specified in main.cpp. No additional declarations required:
MouseArea {
anchors.fill: parent
onClicked: {
myObject.reken_tijden_uit()
}
}
You can find more information on QML<->C++ communication possibilities here: https://v-play.net/cross-platform-development/how-to-expose-a-qt-cpp-class-with-signals-and-slots-to-qml