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. Calling c++ function from qml
Forum Updated to NodeBB v4.3 + New Features

Calling c++ function from qml

Scheduled Pinned Locked Moved QML and Qt Quick
25 Posts 5 Posters 27.1k 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.
  • R Offline
    R Offline
    rockstar
    wrote on last edited by
    #21

    i am using qt creator......

    actually till yesterday i was doing programs only using qml.....this is my first project using both cpp and qml ......

    i thought it was easy but it's not.......

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #22

      It actually is easy.

      Please don't get me wrong - but I have told you for the third time now how it is supposed to be done and how to circumvent the need of including generated files and I still sense that you do not even properly read what I write.
      @
      // testclass.h

      class TestClass : public QObject
      {
      Q_OBJECT

      public:
      TestClass(QObject *parent = 0);

      Q_INVOKABLE QString returnString();
      

      };

      // testclass.cpp

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

      QString TestClass::returnString()
      {
      return "string";
      }

      // main.cpp

      int main(int argc, char *argv[])
      {
      QApplication application(argc, argv);

      QDeclarativeView view;
      
      view.rootContext()->setContextProperty("TestClass", new TestClass(&view));
      view.setSource(QUrl::fromLocalFile("Test.qml"));
      view.show();
      
      return application.exec();
      

      }

      // Test.qml

      import QtQuick 1.1

      Text {
      text: TestClass.returnString()
      }
      @

      1 Reply Last reply
      0
      • R Offline
        R Offline
        rockstar
        wrote on last edited by
        #23

        thanks a ton Lukas.....it worked....

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mathiii
          wrote on last edited by
          #24

          Hey guys,

          I´m having exactly the same problem, but I think some of the Classes and Libraries have changed. The way Lukas explained this makes a lot of sense to me, but the code doesn´t work for me. I´m using Qt 5.3 with QtQuick 2.2 .
          I would be very thankful if someone could look at my code, as I´m desperate at the moment.My goal is to write a String into an xml-file.
          the c++function should be called from qml.

          Thanks in advance

          @
          //header.h
          class TestClass : public QObject
          {
          Q_OBJECT

          public:
          TestClass(QObject *parent = 0);

          Q_INVOKABLE void writeXml(QString &text){ }
          

          };@

          @//func.cpp

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

          void TestClass::writeXml(QString text)
          {

                  QString filename = "C:/Qt5/Tools/QtCreator/bin/CPPTEST2/example.xml";
                  QFile file(filename);
                  if (file.open(QIODevice::ReadWrite)) {
                      QTextStream stream(&file);
                      stream << text << endl;
                       }
                  file.close();
          

          }@

          @
          //main.cpp
          {
          QGuiApplication app(argc, argv);

          QQmlApplicationEngine engine;
          engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
          QQuickView *view = new QQuickView;
          view->setSource(QUrl::fromLocalFile&#40;"qrc:///main.qml"&#41;);
          view->show();
          
          return app.exec();
          

          }
          @
          @ //main.qml
          import QtQuick 2.2
          import QtQuick.Window 2.1

          Window {
          visible: true
          width: 360
          height: 360

          MouseArea {
              anchors.fill: parent
              onClicked: {
                  TestClass.writeXml("Hello World");
              }
          }
          

          }@

          1 Reply Last reply
          0
          • M Offline
            M Offline
            messi
            wrote on last edited by
            #25

            Hi mathiii
            this is not how it works.
            You have to create a C++ object first.
            Than make the object for QML visible.
            Now you can access the object.

            Check out the following link.
            It describes every C++/QML interaction:
            http://qt-project.org/wiki/Introduction_to_Qt_Quick_for_Cpp_developers#e70ea8482b12de725ec779f1bc55c92c

            Best regards

            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