Hello I have already asked the following on Stackoverflow [stackoverflow.com] 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.
↧