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



  • 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

    }
    

    @



  • Have you seen this "doc ( Integrating QML and C++ ) ":https://qt-project.org/doc/qt-5/qtqml-cppintegration-topic.html ?



  • 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]


  • Moderators

    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 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 :(



  • 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&#40;"MyItem.qml"&#41;);
    view.show();
    
    
    return a.exec&#40;&#41;;
    

    }


    // --- 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



  • [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 += 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&#40;"MyItem.qml"&#41;&#41;;
    view.show();
    
    
    return a.exec&#40;&#41;;
    

    }


    // --- 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[/quote]

    Hi , and Very very thank you Prashant!



  • Hi Kamb!z,

    Your Welcome to Qt's cross platform world !!!

    Thanks
    Prashant Moglaikar


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.