Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Add qml object to QGraphicsScene



  • I try to add some QML object to my QGraphcisScene but they don't display in the scene. Here is the code.
    @QList<QObject*> dataList;
    dataList.append(new DataObject("Item 1", "red"));
    dataList.append(new DataObject("Item 2", "green"));

    QDeclarativeEngine engine ;
    QDeclarativeContext *context = engine.rootContext();
    context->setContextProperty("myModel", QVariant::fromValue(dataList));
    QUrl url("qrc:view.qml") ;
    QDeclarativeComponent component(&engine,url ) ;
    QDeclarativeItem *item = qobject_cast <QDeclarativeItem *>(component.create());
    item->setFlag(QGraphicsItem::ItemHasNoContents, false);
    myScene->addItem(item);
    @

    And here is my qml file:
    @ListView {
    width: 100; height: 100

    model: myModel
    delegate: Rectangle {
    height: 25
    width: 100
    color: model.modelData.color
    Text { text: name }
    }@


Log in to reply