June 30, 2011

eirnanG eirnanG
Lab Rat
23 posts

QML to CPP

 

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.

12 replies

June 30, 2011

QtK QtK
Lab Rat
1140 posts

eirnanG wrote:
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.

Please post it in a single section and avoid [developer.qt.nokia.com] multiple posts.

June 30, 2011

Andre Andre
Robot Herder
6393 posts

Did you try the relevant documentation [developer.qt.nokia.com] ?

June 30, 2011

eirnanG eirnanG
Lab Rat
23 posts

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

can you cite some sample codes? or situation?

June 30, 2011

Vijay Bhaska Reddy Vijay Bhaska Reddy
Lab Rat
399 posts

QtK wrote:

eirnanG wrote:
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.

Please post it in a single section and avoid [developer.qt.nokia.com] multiple posts.

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

  1.      QmlApplicationViewer viewer;
  2.      YourQmlObject qmlobject(&viewer);
  3.      QDeclarativeContext * context = viewer.rootContext();
  4.      context->setContextProperty("qmlobject",&basicCalc);

qml side

  1.    qmlobject.callFunction("stringyouwanttopass");

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

Declaration of callFunction is

  1. Q_INVOKABLE void callFunction(QString str);

July 1, 2011

eirnanG eirnanG
Lab Rat
23 posts

what are the codes inside the class YourQmlObject?

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

July 1, 2011

Vijay Bhaska Reddy Vijay Bhaska Reddy
Lab Rat
399 posts

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

July 17, 2011

eirnanG eirnanG
Lab Rat
23 posts

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

please help.. thanks. :)

July 17, 2011

eirnanG eirnanG
Lab Rat
23 posts

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

July 17, 2011

Vijay Bhaska Reddy Vijay Bhaska Reddy
Lab Rat
399 posts

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.

July 18, 2011

eirnanG eirnanG
Lab Rat
23 posts

@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

July 18, 2011

changsheng230 changsheng230
Lab Rat
128 posts

  1.  // MyItem.qml
  2.  import QtQuick 1.0
  3.  
  4.  Item {
  5.      width: 100; height: 100
  6.  
  7.      MouseArea {
  8.          anchors.fill: parent
  9.          onClicked: {
  10.              myObject.cppMethod("Hello from QML")
  11.              myObject.cppSlot(12345)
  12.          }
  13.      }

  1. // CPP code
  2. class MyClass : public QObject
  3.  {
  4.      Q_OBJECT
  5.  public:
  6.      Q_INVOKABLE void cppMethod(const QString &msg;) {
  7.          qDebug() << "Called the C++ method with" << msg;
  8.      }
  9.  
  10.  public slots:
  11.      void cppSlot(int number) {
  12.          qDebug() << "Called the C++ slot with" << number;
  13.      }
  14.  };
  15.  
  16.  int main(int argc, char *argv[]) {
  17.      QApplication app(argc, argv);
  18.  
  19.      QDeclarativeView view;
  20.      MyClass myClass;
  21.      view.rootContext()->setContextProperty("myObject", &myClass;);
  22.  
  23.      view.setSource(QUrl::fromLocalFile("MyItem.qml"));
  24.      view.show();
  25.  
  26.      return app.exec()
  27. }

 Signature 

Chang Sheng
常升

July 19, 2011

eirnanG eirnanG
Lab Rat
23 posts

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

 
  ‹‹ List of Qt Quick examples and demos      Memory cleanup ››

You must log in to post a reply. Not a member yet? Register here!