Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    How do I interact with QML from C++ in QtQuick 2.0

    QML and Qt Quick
    2
    3
    2772
    Loading More Posts
    • 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.
    • I
      Itehnological last edited by

      Let's say that we have a very simple QML file, like this one:

      @import QtQuick 2.0

      Rectangle {
      width: 800
      height: 600
      color: '#000'

      Text {
          text: qsTr("Hi all")
          anchors.centerIn: parent
      }
      

      }
      @

      The QML File is loaded with the QtQuick2ApplicationViewer helper class, like this:
      @QtQuick2ApplicationViewer viewer;
      viewer.setMainQmlFile(QStringLiteral("qml/MyApp/Login/Window.qml"));
      viewer.showFullScreen();
      @

      How should I proceed, if for example I would like to change the Rectangle's color to white, from C++. My guess was:
      @QQuickItem *window = viewer.rootObject();
      window->setProperty("color", "#fff");
      @
      But all that does is the following compiler error:
      @invalid use of incomplete type 'struct QQuickItem'
      forward declaration of 'struct QQuickItem'
      @

      1 Reply Last reply Reply Quote 0
      • H
        hipersayan_x last edited by

        You forgot the:

        @
        #include <QQuickItem>
        @

        before the use.

        1 Reply Last reply Reply Quote 0
        • I
          Itehnological last edited by

          Yhea. Didn't noticed I was missing that library. I spent a whole day digging on why that doesn't work. Thanks a lot!

          1 Reply Last reply Reply Quote 0
          • First post
            Last post