View Full Version : Qml in QGraphicsWidget
animagani
1st December 2010, 08:26
Is it possible to insert QML file into QGraphicsWidget? (Eg. insert QML file into Layout?)
wysota
1st December 2010, 08:48
Yes, fetch the object created by QDeclarativeComponent and do whatever you want with it.
animagani
1st December 2010, 08:56
I've tried something like this:
QDeclarativeEngine *engine = new QDeclarativeEngine;
QDeclarativeComponent component(engine, QUrl("qrc:/qml/main.qml"));
QGraphicsLayoutItem *object =qobject_cast<QGraphicsLayoutItem *>(component.create());
QGraphicsLinearLayout* layout = new QGraphicsLinearLayout();
layout->addItem(object);
But I'm getting:
QGraphicsLinearLayout::insertItem: cannot insert null item
When I cast it to QGraphicsObject I'm getting not null object, but I can't add this to layout.
wysota
1st December 2010, 12:09
You need to place the object in a QGraphicsWidget and insert that graphics widget into the layout.
animagani
1st December 2010, 13:58
It started working, but I have another issue. My qml file code is:
import QtQuick 1.0
Rectangle {
id: page
width: 500; height: 200
color: "lightgray"
Text {
id: helloText
text: "Hello world!"
y: 30
anchors.horizontalCenter: page.horizontalCenter
font.pointSize: 24; font.bold: true
}
}
And when I load this file, put in QGraphicsWidget, it only renders rectangle without text inside.
wysota
1st December 2010, 15:54
Does it work if you show the qml file in a separate view?
animagani
2nd December 2010, 13:12
Probably I'm doing something wrong with constructing QGraphicsWidget from QGraphicsItem.
When I do this:
QDeclarativeEngine *engine = new QDeclarativeEngine();
QDeclarativeComponent component(engine, QUrl("qrc:/main.qml"));
QObject *myObject = component.create();
QGraphicsObject* graphicsObject =qobject_cast<QGraphicsObject *>(myObject);
scene.addItem(graphicsObject);
Everything is ok, but when I'm trying to do it in this way (I need to add qml content to QGraphicsWidget):
main.cpp:
#include <QApplication>
#include <QTextEdit>
#include <QGraphicsWidget>
#include <QGraphicsLinearLayout>
#include <QGraphicsView>
#include "testqml.h"
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
QGraphicsView view;
QGraphicsScene scene;
view.setScene(&scene);
QGraphicsWidget mainWidget;
QGraphicsLinearLayout layout(Qt::Vertical);
TestQML* tkuml = new TestQML();
layout.addItem(tkuml);
mainWidget.setLayout(&layout);
scene.addItem(&mainWidget);
view.show();
return a.exec();
}
testqml.h
#include <QDeclarativeEngine>
#include <QDeclarativeComponent>
#include <QGraphicsWidget>
class TestQML : public QGraphicsWidget
{
Q_OBJECT
QGraphicsObject* graphicsObject;
public:
TestQML()
{
QDeclarativeEngine *engine = new QDeclarativeEngine(this);
QDeclarativeComponent component(engine, QUrl("qrc:/main.qml"));
QObject *myObject = component.create();
graphicsObject =qobject_cast<QGraphicsObject *>(myObject);
}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
graphicsObject->paint(painter, option, widget);
}
QRectF boundingRect() {
return graphicsObject->boundingRect();
}
};
It only renders gray rectangle, but without any content. (QML file in above post, stored in resource file).
wysota
2nd December 2010, 16:20
No, it definitely won't work this way. Instead you should set the qml-originated graphics object as a child of QGraphicsWidget (you don't need to subclass it) and put that widget in a layout.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.