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

<SOLVED> Changing QML properties in C++

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 2 Posters 3.6k 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.
  • R Offline
    R Offline
    reindeernix
    wrote on last edited by
    #1

    Hello I have already asked the following on "Stackoverflow":http://stackoverflow.com/questions/18594459/changing-qml-properties-in-c though, but I will try asking here now :)

    I am trying to change a property(canvasWidth) in a QML program with Qt/C++, only problem is that it won't work. When I change the property and use the debugging from Qt/C++ it says that the property has changed but when I make QML print the property out it says it hasn't changed.

    I have tried to compile with both Clang and GCC on 64bit Ubuntu/Fedora with Qt 5.0.2 and QtCreator 2.7/2.8.

    I am using Fedora 19 64bit with QtCreator 2.8, Qt 5.0.2 and Clang 3.3 as compiler

    I have no idea what I made wrong here and I can't seem to find a solution anywhere so I am asking here.

    I have made this so far.

    QML: (main.qml)

    @
    import QtQuick 2.0

    Item {
    property int canvasWidth: 200
    objectName: "background"

    width: canvasWidth; height: 1000

    Rectangle {
    id: background
    anchors.fill: parent
    width: canvasWidth
    color: "lightgrey"

    Rectangle {
        id: scrollArea
        color: parent.color
        border.color: "grey"
        border.width: 2
        x: 0; y: button1.height
        width: 201; height: 1000
    
        Rectangle {
            color: parent.color
            height: 2
            width: parent.width - 4
            x: 2
            y: 0
        }
    
        MouseArea {
            anchors.fill: parent
            property var scrollValue: 20
            onWheel: {
                if (wheel.angleDelta.y > 0) {
                    button1.y -= scrollValue
                    console.log(canvasWidth)
                }
                if (wheel.angleDelta.y < 0)
                    button1.y += scrollValue
                    console.log(canvasWidth)
            }
    
            Button {
                id: button1
                y: 0
                color: "lightblue"
                border.color: "grey"
            }
    
            Button {
                id: button2
                y: button1.y + height
                color: "lightblue"
                border.color: "grey"
            }
        }
    }
    
    Rectangle {
        id: menuArea
        color: parent.color
        width: parent.width
        height: button1.height
        border.color: "grey"
    }
    

    }
    }
    @

    C++: (main.cpp)
    @
    #include <QtGui/QGuiApplication>
    #include "qtquick2applicationviewer.h"
    #include <QGuiApplication>
    #include <QQuickWindow>
    #include <QQmlEngine>
    #include <QQmlComponent>
    #include <QQmlProperty>
    #include <QDebug>
    #include <QQuickItem>

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

    QGuiApplication app(argc, argv);

    QtQuick2ApplicationViewer viewer;
    viewer.setMainQmlFile(QStringLiteral("qml/EasyCAS/main.qml"));
    viewer.showExpanded();

    //view->setProperty("width", 1000);

    QQmlEngine engine;
    QQmlComponent component(&engine, "qml/EasyCAS/main.qml");
    QObject *object = component.create();

    object->setProperty("canvasWidth", 10000);
    QQmlProperty::write(object, "canvasWidth", 5000);
    qDebug() << "Property value:" << object->property("canvasWidth").toInt();

    //-------------------------------------

    QObject *rootObject = dynamic_cast<QObject *>(viewer.rootObject());

    // find element by name
    QObject *image = rootObject->findChild<QObject *>(QStringLiteral("background"));

    if (image) { // element found
    image->setProperty("canvasWidth", 2000);
    }

    return app.exec();

    //QObject *object = view->rootObject();
    //QQmlProperty(object, "width").write(500);
    }
    @

    Thanks in advance.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      You are creating 2 separate instances of main.qml, then modify the one that is not visible... no wonder it does not work. Also, you are using findChild() and look for item "id" instead of "objectName". Ids are QML-internal and are not visible from C++.

      So basically:

      • line 10 in QML code: add another line:
        @
        objectName: "background"
        @
      • remove lines 19-30, they are not needed
      • set canvasWidth property for rootObject you get in line 32. It might work there (although it's also possible you need to get one level deeper using findChild()). Or even easier: add canvasWidth as global property by using setContextProperty()

      (Z(:^

      1 Reply Last reply
      0
      • R Offline
        R Offline
        reindeernix
        wrote on last edited by
        #3

        Thanks a lot sierdzio :)

        With your help I made it work and because of this I seem to understand how the system works, though I will need some more practice to learn about QObjects etc.

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          No worries :) QML requires a bit different way of thinking than C++, which makes connecting it to C++ a bit tricky even for experienced users. If you need some hardcore example of QtQuick, feel free to browse my CCF project ("link":https://github.com/sierdzio/closecombatfree).

          (Z(:^

          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