[SOLVED] How to call C++ class or function on QML
-
wrote on 5 May 2014, 04:50 last edited by
Hello everybody ;
I want to call C++ class or function into my QML design ... How can I do it ? :) please show me with a example of method for Qt 5.2 or later.
For example this is my class and function in C++ file.
@
class MyClass
{
void MyFunction()
{qDebug() << "This is C++ Code ! but GUI designed by QMl technology"; }
};
@and QML Button code
@
Button {
text: qsTr("Hello World")
anchors.centerIn: parent}
@
-
wrote on 5 May 2014, 05:19 last edited by
Have you seen this "doc ( Integrating QML and C++ ) ":https://qt-project.org/doc/qt-5/qtqml-cppintegration-topic.html ?
-
wrote on 5 May 2014, 06:12 last edited by
Yes , But i want example with source code for QML or HTML5 design.
[quote author="qxoz" date="1399267156"]Have you seen this "doc ( Integrating QML and C++ ) ":https://qt-project.org/doc/qt-5/qtqml-cppintegration-topic.html ?[/quote]
-
Example source code at https://qt-project.org/doc/qt-5/qtqml-cppintegration-contextproperties.html
Note that you can only objects derived from QObject can be accessed directly from QML.
-
wrote on 5 May 2014, 08:34 last edited by
[quote author="JKSH" date="1399275337"]Example source code at https://qt-project.org/doc/qt-5/qtqml-cppintegration-contextproperties.html
Note that you can only objects derived from QObject can be accessed directly from QML.[/quote]
I Check this but show this error :
@
/home/kambiz/Documents/Projects/untitled6/main.cpp:8: error: undefined reference to `vtable for ApplicationData'
@Unfortunately I don't have experience with QML :(
-
wrote on 5 May 2014, 12:49 last edited by
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 += quickgreaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = sample_ttr
TEMPLATE = appSOURCES += main.cpp
HEADERS +=
ApplicationData.hFORMS +=
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.0Text { text: applicationData.getCurrentDateTime() }
Thanks
Prashant -
wrote on 5 May 2014, 14:02 last edited by
[quote author="Prashant Moglaikar" date="1399294151"]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 += quickgreaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = sample_ttr
TEMPLATE = appSOURCES += main.cpp
HEADERS +=
ApplicationData.hFORMS +=
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.0Text { text: applicationData.getCurrentDateTime() }
Thanks
Prashant[/quote]Hi , and Very very thank you Prashant!
-
wrote on 6 May 2014, 04:11 last edited by
Hi Kamb!z,
Your Welcome to Qt's cross platform world !!!
Thanks
Prashant Moglaikar
1/8