Important: Please read the Qt Code of Conduct -

[Solved] How I can add a qml object to QGraphicsScene using QtQuick 2.0?

  • I modify the constructor of DiagramScene in a qt example for see a QML widget but this not work:
    @DiagramScene::DiagramScene(QObject *parent)
    : QGraphicsScene(parent) {
    QQuickView *view = new QQuickView;
    QWidget *container = QWidget::createWindowContainer(view);

    QML(The file path is correct!!):
    @import QtQuick 2.0
    Rectangle {
    width: 100
    height: 100
    color: "black"

  • Moderators

    Can you clarify a bit what is exactly not working here? Is the container widget drawn empty? Is it not being drawn at all? Maybe some compilation errors, or debug warning messages are being shown?

    The first thought that comes to my mind is that you might be using the GraphicsView with raster painting engine, and maybe it should be switched to OpenGL engine before you create the container.

  • The problem is that the container widget drawn empty, I think the the problem is associate with OpenGL, exactly for diferences between the normal QGraphicsItem render an the QML render in the same application, but I no know how solve this.

    The code work ok if I pass the container to a QMainWindows for example, but not to a QGraphicsScene.

    Work system:
    gcc (Debian 4.9.1-4) 4.9.1
    Qt 5.3
    Linux CIAA 3.14-2-amd64 #1 SMP Debian 3.14.15-2 2014-08-09) x86_64 GNU/Linux

  • Moderators

    Please try setting QGraphicsView into OpenGL mode, like "here": You can also try it with QOpenGLWidget if you switch to Qt 5.4.

    I have never tried this, so I give no guarantee it would - or even could - work, but it's definitely worth a try.

  • I try configuring QQuickView into OpenGL mode but not work...
    This is a bug of the framework?

  • Qt Champions 2017

    Try this ?

    @QMLWidget2GraphicsScene::QMLWidget2GraphicsScene(QWidget *parent)
    : QWidget(parent)
    QGraphicsView *view = new QGraphicsView(this);
    QGraphicsScene *scene = new QGraphicsScene;

    QQuickView *view1 = new QQuickView;
    QWidget *qWid = QWidget::createWindowContainer(view1,this);
    for (int x=0;x<100;x+=10){
        for (int y=0;y<100;y+=10){
        QGraphicsEllipseItem *item= new QGraphicsEllipseItem(x,y,8,8);


  • Thank you very much,
    your example works well and based on this I correct my example:
    QWidget * QWidget :: container = createWindowContainer (QuickView); // Fail
    QWidget * QWidget :: container = createWindowContainer (QuickView, graphicsView); // Ok

    The only problem is that I have a message in runtime but it will correct:
    QGraphicsProxyWidget :: setWidget: can not embed widget 0xce8250 Which is not a
    toplevel widget, and is not a child of an embedded widget
    How I can mark this thread as solved?

    [Edit: added code tags. sierdzio]

  • Moderators

    [quote author="adacosta" date="1409843743"]How I can mark this thread as solved?[/quote]

    Edit your first post and add [Solved] to the title.

Log in to reply