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. C++ to change QML object properties
Forum Updated to NodeBB v4.3 + New Features

C++ to change QML object properties

Scheduled Pinned Locked Moved QML and Qt Quick
57 Posts 4 Posters 44.4k 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.
  • V Offline
    V Offline
    Vignes
    wrote on last edited by
    #1

    I want to change QML objects properties using C++.
    I tried the “http://qt-project.org/doc/qt-5/qml-qtqml-qtobject.html”. I couldn’t get it working on QT5.3.
    If anyone could provided working example for QT5.3 would be great help either “QT Declarative” or “QTObject”.

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

      Hi,

      Are you using QQuickView or QQmlApplicationEngine ?

      157

      1 Reply Last reply
      0
      • V Offline
        V Offline
        Vignes
        wrote on last edited by
        #3

        Hi,
        I am using both. below my main.cpp code:

        @
        #include <QGuiApplication>
        #include <QQmlApplicationEngine>
        #include <QObject>
        #include <QtQuick/QQuickView>
        #include <QtQuick/QQuickItem>

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

        QQmlApplicationEngine engine;
        engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
        
        QQuickView view;
        view.setSource(QUrl::fromLocalFile&#40;"main.qml"&#41;&#41;;
        

        // view.show();

        QQuickItem *item = view.rootObject()->findChild<QQuickItem*>("myRect");
        if (item)
            item->setProperty("color", QColor(Qt::yellow));
        
        return app.exec&#40;&#41;;
        

        }

        main.qml code:

        import QtQuick 2.1
        import QtQuick.Window 2.1

        //import QtQuick 2.0
        //import QtQuick.Window 2.0

        Window {
        visible: true
        width: 360
        height: 360

        MouseArea {
            anchors.fill: parent
            onClicked: {
                Qt.quit();
            }
        }
        
        Text {
            text: qsTr("Hello World")
            anchors.centerIn: parent
        }
        Item {
            width: 200; height: 200
        
            Rectangle {
                anchors.fill: parent
                color: "red"
                objectName: "myRect"
            }
        }
        

        }
        @

        I want the color to change.

        [edit: added missing coding tags @ SGaist]

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

          Please encapulate your code in the code tags '@@' so that its easily readable to others.

          You should either use QQmlApplicationEngine or QQuickView not both.
          If using QQmlApplicationEngine you can try
          @
          QGuiApplication app(argc, argv);
          QQmlApplicationEngine engine;
          engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
          QQuickItem item = engine.rootObjects().at(0)->findChild<QQuickItem>("myRect");
          if (item) {
          item->setProperty("color", QColor(Qt::yellow));
          }
          return app.exec();
          @

          157

          S 1 Reply Last reply
          1
          • V Offline
            V Offline
            Vignes
            wrote on last edited by
            #5

            Hi,
            Thank you very much. It works fine.
            Is it possible to control animation using C++?

            I am testing code tags:
            @
            #include <QQmlApplicationEngine>
            //#include <QObject>
            //#include <QtQuick/QQuickView>
            #include <QtQuick/QQuickItem>

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

            QQmlApplicationEngine engine;
            engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
            

            // QQuickView view;
            // view.setSource(QUrl::fromLocalFile("main.qml"));
            // view.show();

            QQuickItem *item = engine.rootObjects().at(0)->findChild<QQuickItem*>("myRect");
            if (item)
                item->setProperty("color", QColor(Qt::yellow));
            
            return app.exec();
            

            }
            @

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

              In the same way as above you can set the "running" property of animation so that you can start or stop the animation or set other properties.

              157

              1 Reply Last reply
              0
              • V Offline
                V Offline
                Vignes
                wrote on last edited by
                #7

                Hi,
                I try to change width and height of object, but I'm couldn't

                qml
                @
                Window {
                visible: true
                width: 360
                height: 360
                objectName: "myWindow"
                }
                @

                main.cpp
                @
                #include <QGuiApplication>
                #include <QQmlApplicationEngine>
                #include <QtQuick/QQuickItem>

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

                QQmlApplicationEngine engine;
                engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
                

                // QQuickView view;
                // view.setSource(QUrl::fromLocalFile("main.qml"));
                // view.show();

                QQuickItem *item = engine.rootObjects().at(0)->findChild<QQuickItem*>("myWindow");
                if (item)
                    item->setProperty("width", QWidth(Qt::560));
                
                return app.exec();
                

                }
                @

                Could you help.

                Thank you

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

                  Hi,

                  There are multiple problems here,

                  1. "Window":http://qt-project.org/doc/qt-5/qml-qtquick-window-window.html doesn't instantiate QQuickItem but QQuickWindow. So you can't cast it to QQuickItem.
                  2. There's nothing like QWidth or Qt::560, just plainly use the number e.g 560
                  3. Window is a top level, so you can get it directly instead of finding.

                  To sum up, try this,
                  @
                  QQuickWindow *item = (QQuickWindow *)engine.rootObjects().first();
                  if (item)
                  item->setProperty("width", 1360);
                  @

                  157

                  1 Reply Last reply
                  0
                  • V Offline
                    V Offline
                    Vignes
                    wrote on last edited by
                    #9

                    Hi,
                    I tried to change QML Window to fullscreen using visibility via c++
                    @
                    QQuickWindow *W = (QQuickWindow *)engine.rootObjects().first();
                    if (W)
                    W->setProperty("visibility", "Fullscreen");
                    @
                    tried
                    @
                    W->FullScreen;
                    @
                    tried
                    @
                    W->visibility() = "Fullscreen";
                    @

                    none of them are working. Help please?

                    1 Reply Last reply
                    0
                    • V Offline
                      V Offline
                      Vignes
                      wrote on last edited by
                      #10

                      Hi,
                      I got it working, it the spelling of the Fullscreen should be "FullScreen"
                      @
                      QQuickWindow *W = (QQuickWindow *)engine.rootObjects().first();
                      if (W)
                      W->setProperty("visibility", "FullScreen");
                      @

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

                        For FullScreen you can use directly,

                        @
                        if (W)
                        W->showFullScreen();
                        @

                        157

                        1 Reply Last reply
                        0
                        • V Offline
                          V Offline
                          Vignes
                          wrote on last edited by
                          #12

                          Hi,
                          I have objects window, text and mouse area to text in qml document and I want to implement in c++: when text area is clicked: if the window is not on fullscreen, it should become fullscreen and the text in the text are should change to "Window is Fullscreen" with a animated color chage to "white". Next time it clicked it should come out of fullscreen and chanege the text and color. Also console.log equivalent output of the this event along with screen resolution should be output from c++ (not via qml).

                          main.qml :
                          @
                          import QtQuick 2.2
                          import QtQuick.Window 2.1

                          Window {
                          id: window1
                          visible: true
                          width: 360
                          height: 360
                          color: "red"
                          title: "MyWindow"

                          Text {
                              objectName: "myText"
                              text: qsTr("Hello World!<br><i>I'm </i><b>not Fullscreen<i> Now</i></b>")
                              font.bold: true
                              horizontalAlignment: Text.AlignHCenter
                              font.family: "Times New Roman"
                              font.pointSize: 18
                              anchors.centerIn: parent
                              color: "steelblue"
                          
                              MouseArea {
                                  anchors.fill: parent
                              }
                          }
                          

                          }
                          @

                          main.cpp
                          @
                          #include <QGuiApplication>
                          #include <QQmlApplicationEngine>
                          #include <QtQuick/QQuickItem>
                          #include <QtQuick/QQuickWindow>

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

                          QQmlApplicationEngine engine;
                          engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
                          

                          //here I need function for
                          //1 clicke event
                          //check fullscreen value
                          //2 text changing
                          //3 animation
                          //4 console.log output of Screen resolution and action
                          //5 finaly Window : showMaximized(); or showFullScreen();

                          return app.exec();
                          

                          }
                          @

                          Could you help me with this please?
                          Thank you

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

                            You can,

                            1. Create a QObject based class
                            2. Create a SLOT in it
                            3. Pass QQuickWindow pointer (which we obtained as shown above) to it
                            4. Create a signal in your QML
                            5. Do the connections
                            6. In the created class cast the QObject pointer to QQuickWindow using qobject_cast
                            7. Use this pointer to access/modify the properties of the QML window in the SLOT when the SIGNAL is fired from QML

                            157

                            1 Reply Last reply
                            0
                            • V Offline
                              V Offline
                              Vignes
                              wrote on last edited by
                              #14

                              Hi,
                              I created QObject: called iobject.h
                              @
                              #ifndef IOBJECT_H
                              #define IOBJECT_H

                              #include <QObject>

                              class iObject : public QObject
                              {
                              Q_OBJECT
                              Q_PROPERTY(QString name READ name WRITE setName)
                              public:
                              explicit iObject(QObject *parent = 0);

                              QString name() const;
                              
                                  Q_INVOKABLE void nameChang();
                              

                              signals:
                              void nameChanged(int newValue);

                              public slots:
                              void setName(const QString &name);

                              private:
                              QString m_name;
                              };

                              #endif // IOBJECT_H
                              @

                              iobject.cpp
                              @
                              #include "iobject.h"

                              iObject::iObject(QObject *parent) :
                              QObject(parent)
                              {
                              }
                              QString iObject::name() const
                              {
                              return m_name;
                              }
                              iObject::setName(const QString &name){
                              if(name != m_name)
                              m_name = name;
                              emit nameChanged(name);
                              }
                              @

                              main.cpp
                              @
                              #include "iobject.h"
                              #include <QGuiApplication>
                              #include <QQmlApplicationEngine>

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

                              qmlRegisterType<iObject>("myObject", 1, 0, "iObject");
                              
                              QQmlApplicationEngine engine;
                              engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
                              
                              return app.exec();
                              

                              }
                              @

                              main.qml
                              @
                              import QtQuick 2.2
                              import QtQuick.Window 2.1
                              import myObject 1.0

                              Window {
                              visible: true
                              width: 360
                              height: 360
                              title: iObject.name

                              MouseArea {
                                  anchors.fill: parent
                                  onClicked: {
                                      Qt.quit();
                                  }
                              }
                              
                              Text {
                                  text: qsTr("Hello World!<br><i>I'm </i><b>not Fullscreen<i> Now</i></b>")
                                  horizontalAlignment: Text.AlignHCenter
                                  font.family: "Times New Roman"
                                  font.pointSize: 18
                                  anchors.centerIn: parent
                                  color: "steelblue"
                              
                                  MouseArea {
                                      anchors.fill: parent
                                  }
                              }
                              

                              }
                              @

                              At this point, It did not work. erros on main.cpp 'qmlRegisterType'
                              what am I doing wrong?

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

                                Just create a the class and pass the QQuickWindow pointer. No need use qmlRegisterType.
                                Something like this,
                                main.cpp
                                @
                                QQmlApplicationEngine engine;
                                engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
                                QQuickWindow *W = (QQuickWindow *)engine.rootObjects().first();
                                iObject a(W);
                                QObject::connect(engine.rootObjects().first(),SIGNAL(callCpp()),&a,SLOT(onCallCpp()));
                                @

                                No need of custom properties in the class.
                                Remember to cast back QObject back to QQuickWindow in iObject and then you can modify QQuickWindow properties as shown earlier.

                                157

                                1 Reply Last reply
                                0
                                • V Offline
                                  V Offline
                                  Vignes
                                  wrote on last edited by
                                  #16

                                  I'm getting error "main.obj:-1: error: LNK2019: unresolved external symbol "public: __cdecl iObject::iObject(class QObject *)" (??0iObject@@QEAA@PEAVQObject@@@Z) referenced in function main"

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

                                    Have you included its corresponding header file (probably iobject.h) in main.cpp ?

                                    157

                                    1 Reply Last reply
                                    0
                                    • V Offline
                                      V Offline
                                      Vignes
                                      wrote on last edited by
                                      #18

                                      Yes
                                      main.cpp
                                      @
                                      #include "iobject.h"
                                      #include <QGuiApplication>
                                      #include <QQmlApplicationEngine>
                                      #include <QtQuick/QQuickWindow>

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

                                      QQmlApplicationEngine engine;
                                      engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
                                      
                                      QQuickWindow *W = (QQuickWindow *)engine.rootObjects().first();
                                      iObject a(W);
                                      QObject::connect(engine.rootObjects().first(),SIGNAL(callCpp()),&a,SLOT(onCallCpp()));
                                      
                                      return app.exec();
                                      

                                      }
                                      @

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

                                        Can you post the new iobject.cpp and .h file ?

                                        157

                                        1 Reply Last reply
                                        0
                                        • V Offline
                                          V Offline
                                          Vignes
                                          wrote on last edited by
                                          #20

                                          iobject.h
                                          @
                                          #ifndef IOBJECT_H
                                          #define IOBJECT_H

                                          #include <QObject>

                                          class iObject : public QObject
                                          {
                                          Q_OBJECT
                                          public:
                                          explicit iObject(QObject *parent = 0);

                                          signals:

                                          public slots:

                                          };

                                          #endif // IOBJECT_H
                                          @

                                          iobject.cpp
                                          @
                                          #include "iobject.h"

                                          iObject::iObject(QObject *parent) :
                                          QObject(parent)
                                          {
                                          }
                                          @

                                          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