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);
QDeclarativeEngine *engine = new QDeclarativeEngine();
QDeclarativeComponent component(engine, QUrl("qrc:/main.qml"));
QObject *myObject = component.create();
QGraphicsObject* graphicsObject =qobject_cast<QGraphicsObject *>(myObject);
scene.addItem(graphicsObject);
To copy to clipboard, switch view to plain text mode
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[])
{
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();
}
#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();
}
To copy to clipboard, switch view to plain text mode
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);
}
graphicsObject->paint(painter, option, widget);
}
return graphicsObject->boundingRect();
}
};
#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();
}
};
To copy to clipboard, switch view to plain text mode
It only renders gray rectangle, but without any content. (QML file in above post, stored in resource file).
Bookmarks