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



  • 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'
    @



  • You forgot the:

    @
    #include <QQuickItem>
    @

    before the use.



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.