PDA

View Full Version : QDeclarativeItem not changing colors when event is fired in QML



hkenn124
27th February 2011, 21:11
Hi,

I defined a QML file that changes the color of a mousearea's parent when it is clicked (based off of the sample from the documentation). For some reason, the color change that is suppose to happen never happens. The application that I am creating is not a pure QML app (works in that scenario). Instead, I have a qt application with a qgraphicsscene. I am loading the qml as a QdeclarativeItem and adding it into the scene. Does anyone know what I am doing wrong?

Thanks.

QT code:

QGraphicsScene* test = new QGraphicsScene();
QDeclarativeEngine engine;

QDeclarativeComponent component(&engine, QUrl("qrc:/qml/test.qml"));

QDeclarativeItem *item = qobject_cast<QDeclarativeItem *>(component.create());

qDebug() << component.errors();
test->addItem(item);
designWidget.graphicsView->setScene(test);

QML:

import QtQuick 1.0


Rectangle {
id: container
width: 600; height: 200

Rectangle {
id: rect
width: 50; height: 50
color: "red"
opacity: (600.0 - rect.x) / 600

MouseArea {
anchors.fill: parent
drag.target: rect
drag.axis: Drag.XandYAxis
drag.minimumX: 0
drag.maximumX: container.width - rect.width
drag.minimumY: 0
drag.maximumY: container.height - rect.height

acceptedButtons: Qt.LeftButton | Qt.RightButton
onClicked: {
parent.color = "blue"
}

}
}
}

wysota
27th February 2011, 22:59
Doesn't the engine go out of scope when you leave the method?

hkenn124
27th February 2011, 23:45
Thanks. That was it. I really am a newb :) I guess that is what I get for cutting and pasting.

Thanks Again!