Showing items to qml from .cpp
-
i would like to show an item from my cpp to qml, and editing the width of the item but it only shows a black screen,
here is my code
@
//main.cpp
#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include <QDeclarativeEngine>
#include <QDeclarativeComponent>
#include <QDeclarativeProperty>
#include <QDebug>
#include <QObject>
#include <QGraphicsObject>int main(int argc, char *argv[])
{
QApplication app(argc, argv);QDeclarativeView view; view.setSource(QUrl::fromLocalFile("qml1.qml")); view.show(); QObject *object = view.rootObject(); object->setProperty("width", 500); QDeclarativeProperty(object, "width").write(500); return app.exec();
}
@@
//qml1.qml
import QtQuick 1.0Rectangle{
id: object
width: 360
height: 360
color: "#d73c3c"
Item {
x: 46
y: 51
width: 254
height: 100
anchors.rightMargin: 60
anchors.bottomMargin: 72
anchors.leftMargin: 46
anchors.topMargin: 51
anchors.fill: parent
}
}
@ -
"CPP 2 QML Bindings":http://doc.qt.nokia.com/4.7-snapshot/qtbinding.html
I think you need to take a closer look at the doc above to make it work...
-
Notice QObject is not a widget. Have a look at "QML C++ Extensions":http://doc.qt.nokia.com/4.7-snapshot/qdeclarativeexamples.html#c-extensions if you want to create a widget in C++ side
-
is the qml1.qml file in the same folder with your executable ?
-
yes they were in the same folder
-
I copied your code, and run in my Qt simulator, showing a red rectangle. Your code should be all right
[quote author="jr_jags" date="1311644078"]yes they were in the same folder[/quote]