Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Call C++ function from QML
Forum Updated to NodeBB v4.3 + New Features

Call C++ function from QML

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 18.3k Views 1 Watching
  • 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.
  • Y Offline
    Y Offline
    YEAHYEAH
    wrote on last edited by
    #1

    Hi

    I followed this example (http://qt-project.org/doc/qt-5.0/qtqml/qtqml-cppintegration-exposecppattributes.html) but I can't make it run.

    Can someone provide me a complete example of how call a c++ function from qml 2 (source code: cpp, qml, pro ).

    Thanks in advance.

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

      Although there are lot of example's related to this, here's a super simple example which call cpp function from QML on mouse click.

      hellocpp.h

      @
      #ifndef HELLOCPP_H
      #define HELLOCPP_H

      #include <QObject>

      class HelloCpp : public QObject
      {
      Q_OBJECT
      public:
      explicit HelloCpp(QObject *parent = 0);
      Q_INVOKABLE void printMessage(QString txt);

      signals:

      public slots:

      };

      #endif // HELLOCPP_H
      @

      hellocpp.cpp

      @
      #include "hellocpp.h"
      #include <QDebug>

      HelloCpp::HelloCpp(QObject *parent) :
      QObject(parent)
      {
      }

      void HelloCpp::printMessage(QString txt)
      {
      qDebug() << "Message from QML: " << txt;
      }
      @

      main.qml

      @
      import QtQuick 2.0
      import HelloCpp 1.0

      Rectangle {
      width: 360
      height: 360

      HelloCpp {
          id: demo
      }
      
      Text {
          text: qsTr("Hello World")
          anchors.centerIn: parent
      }
      MouseArea {
          anchors.fill: parent
          onClicked: {
              demo.printMessage("Hello Cpp. Nice to see you");
          }
      }
      

      }
      @

      main.cpp

      @
      #include <QtGui/QGuiApplication>
      #include <QtQuick>
      #include "qtquick2applicationviewer.h"
      #include "hellocpp.h"

      int main(int argc, char *argv[])
      {
      QGuiApplication app(argc, argv);

      qmlRegisterType<HelloCpp>("HelloCpp", 1, 0, "HelloCpp");
      
      QtQuick2ApplicationViewer viewer;
      viewer.setMainQmlFile&#40;QStringLiteral("qml/QMLTest/main.qml"&#41;);
      viewer.showExpanded();
      
      return app.exec();
      

      }
      @

      Just go through the code starting from main.cpp. Create a default QtQuick example from QtCreator and copy the above code to it.

      The explanation for the functions like qmlRegisterType and Q_INVOKABLE are already nicely documented. Please refer docs for more details.

      157

      1 Reply Last reply
      1
      • Y Offline
        Y Offline
        YEAHYEAH
        wrote on last edited by
        #3

        Thanks p3c0, your example runs like a charm.

        :D

        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