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. QML to CPP
Forum Updated to NodeBB v4.3 + New Features

QML to CPP

Scheduled Pinned Locked Moved QML and Qt Quick
13 Posts 5 Posters 6.0k 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.
  • K Offline
    K Offline
    kamalakshantv
    wrote on 30 Jun 2011, 10:52 last edited by
    #2

    [quote author="eirnanG" date="1309430672"]Good Day!

    How to pass a value or string from QML to CPP.
    It is like, when i clicked the MouseArea from QML it will send a number/string to cpp. And cpp will do some calculation.

    Please Help.[/quote]

    Please post it in a single section and "avoid":http://developer.qt.nokia.com/forums/viewthread/7325 multiple posts.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on 30 Jun 2011, 10:57 last edited by
      #3

      Did you try the "relevant documentation":http://developer.qt.nokia.com/doc/qt-4.7/qml-extending.html ?

      1 Reply Last reply
      0
      • E Offline
        E Offline
        eirnanG
        wrote on 30 Jun 2011, 11:16 last edited by
        #4

        its irrelevant i think? or i just dont understand that.

        can you cite some sample codes? or situation?

        1 Reply Last reply
        0
        • T Offline
          T Offline
          thisisbhaskar
          wrote on 30 Jun 2011, 11:29 last edited by
          #5

          [quote author="QtK" date="1309431176"]
          [quote author="eirnanG" date="1309430672"]Good Day!

          How to pass a value or string from QML to CPP.
          It is like, when i clicked the MouseArea from QML it will send a number/string to cpp. And cpp will do some calculation.

          Please Help.[/quote]

          Please post it in a single section and "avoid":http://developer.qt.nokia.com/forums/viewthread/7325 multiple posts.
          [/quote]

          this "avoid" path does not exist.

          The link Andre posted has all the required information and has sample code also :).

          Basically you create a QObject derived class with a QML callable function ( By placing Q_INVOKABLE before it), and then create an instance of that class inside your c++ code and make the created object available in qml by exporting it with QDeclarativeContext::setContextProperty, and call qml invokable function from QML side when you want to pass data

          Example :
          C++ Code
          @
          QmlApplicationViewer viewer;
          YourQmlObject qmlobject(&viewer);
          QDeclarativeContext * context = viewer.rootContext();
          context->setContextProperty("qmlobject",&basicCalc);
          @

          qml side
          @
          qmlobject.callFunction("stringyouwanttopass");
          @

          callFunction is declared and defined in your YourQmlObject class and YourQmlObject is a QObject derived class.

          Declaration of callFunction is

          @Q_INVOKABLE void callFunction(QString str);@

          1 Reply Last reply
          0
          • E Offline
            E Offline
            eirnanG
            wrote on 1 Jul 2011, 09:32 last edited by
            #6

            what are the codes inside the class YourQmlObject?

            i think thats what i need but i cant use it well..

            1 Reply Last reply
            0
            • T Offline
              T Offline
              thisisbhaskar
              wrote on 1 Jul 2011, 09:40 last edited by
              #7

              Ok.. I would suggest you to visit page
              http://developer.qt.nokia.com/wiki/Introduction_to_Qt_Quick_for_Cpp_developers

              and then search for "Calling C++ methods from QML", you will get the answer you are looking for :)

              1 Reply Last reply
              0
              • E Offline
                E Offline
                eirnanG
                wrote on 17 Jul 2011, 10:37 last edited by
                #8

                i already tried it.. but lack of codes.. like, how can i display it, what are the codes inside the cpp..

                please help.. thanks. :)

                1 Reply Last reply
                0
                • E Offline
                  E Offline
                  eirnanG
                  wrote on 17 Jul 2011, 10:56 last edited by
                  #9

                  @Vijay Bhaska Reddy can give me ur email so i can send you the whole codes? to see it personally?

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    thisisbhaskar
                    wrote on 17 Jul 2011, 16:52 last edited by
                    #10

                    You can copy/paste the code here... others can also have a look and advise you something :) or

                    you can send the code to my mail id by selecting "Send mail" link in my profile page.

                    1 Reply Last reply
                    0
                    • E Offline
                      E Offline
                      eirnanG
                      wrote on 18 Jul 2011, 02:09 last edited by
                      #11

                      @vijay.. im planning to pass a value from QML when the mousearea is clicked and pass it to CPP to store it..

                      mousearea>clicked>pass an integer=5>store it in CPP>compute etc... >display in QML.. like that..

                      thanks

                      1 Reply Last reply
                      0
                      • C Offline
                        C Offline
                        changsheng230
                        wrote on 18 Jul 2011, 05:13 last edited by
                        #12

                        @
                        // MyItem.qml
                        import QtQuick 1.0

                        Item {
                        width: 100; height: 100

                         MouseArea {
                             anchors.fill: parent
                             onClicked: {
                                 myObject.cppMethod("Hello from QML")
                                 myObject.cppSlot(12345)
                             }
                         }
                        

                        @

                        @
                        // CPP code
                        class MyClass : public QObject
                        {
                        Q_OBJECT
                        public:
                        Q_INVOKABLE void cppMethod(const QString &msg;) {
                        qDebug() << "Called the C++ method with" << msg;
                        }

                        public slots:
                        void cppSlot(int number) {
                        qDebug() << "Called the C++ slot with" << number;
                        }
                        };

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

                         QDeclarativeView view;
                         MyClass myClass;
                         view.rootContext()->setContextProperty("myObject", &myClass;);
                        
                         view.setSource(QUrl::fromLocalFile&#40;"MyItem.qml"&#41;&#41;;
                         view.show();
                        
                         return app.exec&#40;&#41;
                        

                        }
                        @

                        Chang Sheng
                        常升

                        1 Reply Last reply
                        0
                        • E Offline
                          E Offline
                          eirnanG
                          wrote on 19 Jul 2011, 06:22 last edited by
                          #13

                          @all thanks to your response..
                          @changsheng230 thanks! ill try it :)

                          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