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. Change the properties of qml with cpp in run*time
Forum Updated to NodeBB v4.3 + New Features

Change the properties of qml with cpp in run*time

Scheduled Pinned Locked Moved General and Desktop
29 Posts 2 Posters 11.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.
  • A Offline
    A Offline
    AcerExtensa
    wrote on last edited by
    #2

    You can use signal/slots method for communication between QML & cpp. "Using QML Bindings in C++ Applications":http://doc.qt.nokia.com/4.7-snapshot/qtbinding.html

    God is Real unless explicitly declared as Integer.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      AcerExtensa
      wrote on last edited by
      #3

      "Connecting Qt signal to QML function":http://www.developer.nokia.com/Community/Wiki/Connecting_Qt_signal_to_QML_function

      God is Real unless explicitly declared as Integer.

      1 Reply Last reply
      0
      • ? This user is from outside of this forum
        ? This user is from outside of this forum
        Guest
        wrote on last edited by
        #4

        could you help me with an example. I appreciate it.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          AcerExtensa
          wrote on last edited by
          #5

          Just open the links I have send you, where is examples on how to do that....

          Examples from first link:
          @
          QObject rect = object->findChild<QObject>("rect");
          if (rect)
          rect->setProperty("color", "red");
          @

          another one:
          @
          object->setProperty("width", 500);
          QDeclarativeProperty(object, "width").write(500);
          @

          The second link is an step-by-step Tutorial with complete code...

          God is Real unless explicitly declared as Integer.

          1 Reply Last reply
          0
          • ? This user is from outside of this forum
            ? This user is from outside of this forum
            Guest
            wrote on last edited by
            #6

            Oh thank you.. it was not loading earlier. let me have a look and get back..

            1 Reply Last reply
            0
            • ? This user is from outside of this forum
              ? This user is from outside of this forum
              Guest
              wrote on last edited by
              #7

              I dont understand what is on that link... could u help me out with my example.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                AcerExtensa
                wrote on last edited by
                #8

                Post your current code and explain exactly what you want. which function should change which variable in which qml code.

                God is Real unless explicitly declared as Integer.

                1 Reply Last reply
                0
                • ? This user is from outside of this forum
                  ? This user is from outside of this forum
                  Guest
                  wrote on last edited by
                  #9

                  Test.qml

                  @import QtQuick 1.1

                  Rectangle

                  {
                  id:rect1
                  width: 400; height:400
                  color:"grey"
                  property string i:"asdff"
                  property int j:25
                  property int u:0
                  visible:true
                  signal clicked()

                   Rectangle {
                       id:rect2
                       objectName: "rect"
                       anchors.top: parent.top
                       anchors.topMargin: 45
                       anchors.left: parent.left
                       anchors.leftMargin: 35
                       anchors.right: parent.right
                       anchors.rightMargin: 50
                       height: 30
                       radius: 8
                       color:"blue"
                  
                  
                  
                       TextEdit {
                  
                           id:textinput1
                           //text:"Enter the value to pass from QMl to cpp"
                           width: 219
                           height: 30
                           anchors.rightMargin: 96
                           anchors.fill: rect2
                          visible:true
                  
                  
                           TextInput{id:qw
                               anchors.fill:textinput1
                               font.pixelSize: 25
                               text:""
                            onAccepted: [object.cppSlot(qw.text),
                                         object.cppMethod(qw.text)
                                          ]
                  

                  }}}}
                  @

                  1 Reply Last reply
                  0
                  • ? This user is from outside of this forum
                    ? This user is from outside of this forum
                    Guest
                    wrote on last edited by
                    #10

                    @#include <QtCore/QCoreApplication>
                    #include <QtGui/QApplication>
                    #include <QDeclarativeView>
                    #include <QDeclarativeProperty>
                    #include <QGraphicsObject>
                    #include <QObject>
                    #include "MyClass.h"
                    #include <QDebug>
                    #include <QDeclarativeContext>

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

                    //
                     QApplication app(argc, argv);
                     QDeclarativeView view;
                     view.setSource(QUrl::fromLocalFile&#40;"Test.qml"&#41;);
                     view.show();
                     // to change the property of the parent
                     QObject *object = view.rootObject();
                     object->setProperty("width", 500);
                     QDeclarativeProperty(object, "width").write(500);
                    
                     ///// to change the property of the children
                    
                     QObject *rect = object->findChild<QObject*>("rect");
                     if (rect)
                         rect->setProperty("color", "lightblue");
                    

                    // QObject rect1 = object->findChild<QObject>("pop");
                    // if (rect1)
                    // rect1->setProperty("text", "1234560");

                     //
                     MyClass myClass;
                     view.rootContext()->setContextProperty("object", &myClass);
                     return app.exec&#40;&#41;;
                    

                    }

                    @

                    1 Reply Last reply
                    0
                    • ? This user is from outside of this forum
                      ? This user is from outside of this forum
                      Guest
                      wrote on last edited by
                      #11

                      @#ifndef MYCLASS_H
                      #define MYCLASS_H
                      #include <QtGui/QApplication>
                      #include <QDeclarativeView>
                      #include <QDeclarativeProperty>
                      #include <QGraphicsObject>
                      #include <QObject>
                      #include "MyClass.h"
                      #include <QDebug>
                      #include <QDeclarativeContext>

                      class MyClass : public QObject
                      {
                      Q_OBJECT

                      //  if you want to call a method from QML give it as INVOKABLE
                      

                      public:
                      Q_INVOKABLE void cppMethod(const QString &msg) {

                           qDebug() << "FROM QML" << msg;
                       }
                      

                      public:
                      Q_INVOKABLE void cppMethod1(int abc=100) {
                      qDebug() << abc;

                      }
                      

                      public slots:
                      void cppSlot(int number) {
                      qDebug() << "Function call from QML" << number;
                      }
                      };
                      #endif // MYCLASS_H
                      @

                      1 Reply Last reply
                      0
                      • ? This user is from outside of this forum
                        ? This user is from outside of this forum
                        Guest
                        wrote on last edited by
                        #12

                        Here I would like to change the text parameter in cpp during runtime and it should refelect in qml during runtime..
                        Thanks a ton for the help

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          AcerExtensa
                          wrote on last edited by
                          #13

                          You don't have object "rect", you have "rect1" and "rect2"
                          rect1 - is your rootObject
                          rect2 - is children of rootObject,

                          1. Don't use such words like "object" it may be allready registered.

                          2. Do your onAccepted state gets called? Check it with Console.log("test")

                          3. Small example how to use properties and slots:

                          QmlApplicationViewer is just the QDeclarativeView subclass.

                          main.cpp
                          @
                          Q_DECL_EXPORT int main(int argc, char *argv[])
                          {
                          QScopedPointer<QApplication> app(createApplication(argc, argv));

                          QmlApplicationViewer viewer;
                          viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
                          viewer.setMainQmlFile&#40;QLatin1String("qml/simple/main.qml"&#41;);
                          
                          MyClass mcls;
                          viewer.rootContext()->setContextProperty("myClass", &mcls);
                          viewer.showExpanded();
                          mcls.set_fullname("admin");
                          return app->exec&#40;&#41;;
                          

                          }
                          @

                          myclass.h
                          @
                          class MyClass : public QObject
                          {
                          Q_OBJECT
                          Q_PROPERTY(QString fullname READ fullname WRITE set_fullname NOTIFY fullname_changed)

                          public:
                          explicit MyClass(QObject *parent = 0):QObject(parent){}
                          QString fullname(){return this->fname;}

                          public slots:
                          void set_fullname(const QString & str){qDebug() << str; this->fname = str; emit this->fullname_changed(this->fname);}

                          signals:
                          void fullname_changed(const QString &);

                          private:
                          QString fname;
                          };
                          @

                          main.qml
                          @
                          import QtQuick 1.1

                          Rectangle {
                          width: 360
                          height: 360
                          Text {
                          text: myClass.fullname
                          anchors.centerIn: parent
                          }
                          MouseArea {
                          anchors.fill: parent
                          onClicked: {
                          myClass.set_fullname("from qml");
                          }
                          }
                          }
                          @

                          God is Real unless explicitly declared as Integer.

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            AcerExtensa
                            wrote on last edited by
                            #14

                            Here you can download the test project from the post below: "Simple Project":http://vip2006.net/simple.tar.gz

                            God is Real unless explicitly declared as Integer.

                            1 Reply Last reply
                            0
                            • 1 Offline
                              1 Offline
                              143U
                              wrote on last edited by
                              #15

                              @
                              public slots:
                              void set_fullname(const QString & str)
                              {
                              qDebug() << str;
                              this->fname = str;
                              emit this->fullname_changed(this->fname);
                              }
                              @
                              is giving an error,,

                              [Edit: Added @ code formatting -- mlong]

                              1 Reply Last reply
                              0
                              • A Offline
                                A Offline
                                AcerExtensa
                                wrote on last edited by
                                #16

                                I can't read your mind! Which error??? Something about QDebug? Add #include <QDebug>

                                God is Real unless explicitly declared as Integer.

                                1 Reply Last reply
                                0
                                • ? This user is from outside of this forum
                                  ? This user is from outside of this forum
                                  Guest
                                  wrote on last edited by
                                  #17

                                  emit this->fullname_changed(this->fname);

                                  This is giving an error

                                  1 Reply Last reply
                                  0
                                  • A Offline
                                    A Offline
                                    AcerExtensa
                                    wrote on last edited by
                                    #18

                                    Which error???

                                    God is Real unless explicitly declared as Integer.

                                    1 Reply Last reply
                                    0
                                    • ? This user is from outside of this forum
                                      ? This user is from outside of this forum
                                      Guest
                                      wrote on last edited by
                                      #19

                                      expected token ; got this-> i tried several things but not workin

                                      1 Reply Last reply
                                      0
                                      • A Offline
                                        A Offline
                                        AcerExtensa
                                        wrote on last edited by
                                        #20

                                        Can you just copy the whole error here?
                                        Delete "this->" or delete whole emit statement it is not relevant in this example....

                                        God is Real unless explicitly declared as Integer.

                                        1 Reply Last reply
                                        0
                                        • ? This user is from outside of this forum
                                          ? This user is from outside of this forum
                                          Guest
                                          wrote on last edited by
                                          #21

                                          Can u help me with this example. I understand this better.
                                          .qml file
                                          @Rectangle {
                                          id:rect1
                                          signal dataRequired;
                                          signal onchangecolor;
                                          width:250
                                          height:250

                                          function updateData(text) { dataText.text = text}  // slot
                                          function updatecolor(color){rect1.color="red"}
                                          
                                          anchors.fill: parent; color: "black"
                                          
                                          Text {
                                              id: dataText
                                              anchors.centerIn: parent; color: "white"
                                          }
                                          
                                          MouseArea {
                                              anchors.fill: parent
                                              onClicked:[ dataRequired(),
                                                  onchangecolor()]
                                          }
                                          

                                          }@

                                          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