Quantcast
Channel: Qt DevNet forums: Qt Quick 1283365070**
Viewing all articles
Browse latest Browse all 4972

[SOLVED] How to call C++ class or function on QML

$
0
0
Hi Kamb!z, Here is the working example I have complied and run it ……. ! // —— sample_ttr.pro —- #————————————————————————- # Project created by QtCreator 2014-05-05T18:05:02 # #————————————————————————- QT += core gui QT += quick greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = sample_ttr TEMPLATE = app SOURCES += main.cpp HEADERS += \ ApplicationData.h FORMS += OTHER_FILES += \ MyItem.qml —————————————————————————————————————— // —— main.cpp —- #include <QApplication> #include <QtQuick/QQuickView> #include <QQmlContext> #include “ApplicationData.h” int main(int argc, char *argv[]) { QApplication a(argc, argv); QQuickView view; ApplicationData data; view.rootContext()->setContextProperty(“ApplicationData”, &data); view.setSource(QUrl::fromLocalFile(“MyItem.qml”)); view.show(); return a.exec(); } ——————————————————————————————————————— // —- ApplicationData.h —- #ifndef APPLICATIONDATA_H #define APPLICATIONDATA_H #include <QObject> #include <QDateTime> class ApplicationData : public QObject { Q_OBJECT public: Q_INVOKABLE QDateTime getCurrentDateTime() const { return QDateTime::currentDateTime(); } }; #endif // APPLICATIONDATA_H ——————————————————————————————————————- // —— MyItem.qml —- // MyItem.qml import QtQuick 2.0 Text { text: applicationData.getCurrentDateTime() } ———————————————————————————————————————————— Thanks Prashant Hi , and Very very thank you Prashant!

Viewing all articles
Browse latest Browse all 4972

Trending Articles