Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [Solved] How I can add a qml object to QGraphicsScene using QtQuick 2.0?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved QML and Qt Quick
8 Posts 3 Posters 4.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    adacosta
    wrote on last edited by
    #1

    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;
    view->setResizeMode(QQuickView::SizeRootObjectToView);
    view->setSource(QUrl("qrc:///QML/qml/item.qml"));
    QWidget *container = QWidget::createWindowContainer(view);
    container->setMinimumSize(view->size());
    container->setFocusPolicy(Qt::TabFocus);
    this->addWidget(container);
    }@

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

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      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.

      (Z(:^

      1 Reply Last reply
      0
      • A Offline
        A Offline
        adacosta
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Please try setting QGraphicsView into OpenGL mode, like "here":http://www.qtcentre.org/threads/40987-QGraphicsScene-OpenGL-rendering. 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.

          (Z(:^

          1 Reply Last reply
          0
          • A Offline
            A Offline
            adacosta
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • dheerendraD Offline
              dheerendraD Offline
              dheerendra
              Qt Champions 2022
              wrote on last edited by
              #6

              Try this ?

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

              QQuickView *view1 = new QQuickView;
              view1->setSource(QUrl("qrc:///Test.qml"));
              QWidget *qWid = QWidget::createWindowContainer(view1,this);
              scene->addWidget(qWid);
              
              for (int x=0;x<100;x+=10){
                  for (int y=0;y<100;y+=10){
                  QGraphicsEllipseItem *item= new QGraphicsEllipseItem(x,y,8,8);
                  item->setBrush(Qt::magenta);
                  scene->addItem(item);
                  }
              }
              view->setScene(scene);
              

              }
              @

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              1 Reply Last reply
              0
              • A Offline
                A Offline
                adacosta
                wrote on last edited by
                #7

                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]

                1 Reply Last reply
                0
                • sierdzioS Offline
                  sierdzioS Offline
                  sierdzio
                  Moderators
                  wrote on last edited by
                  #8

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

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

                  (Z(:^

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved