Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [SOLVED] How to call C++ class or function on QML
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved QML and Qt Quick
8 Posts 4 Posters 3.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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

    }
    

    @

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      qxoz
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        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]

        1 Reply Last reply
        0
        • JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          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.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • P Offline
              P Offline
              pmoglaikar
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                [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!

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  pmoglaikar
                  wrote on last edited by
                  #8

                  Hi Kamb!z,

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

                  Thanks
                  Prashant Moglaikar

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved