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. Add qml object to QGraphicsScene
Qt 6.11 is out! See what's new in the release blog

Add qml object to QGraphicsScene

Scheduled Pinned Locked Moved QML and Qt Quick
1 Posts 1 Posters 730 Views 1 Watching
  • 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.
  • W Offline
    W Offline
    Wahsekim
    wrote on last edited by
    #1

    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 }
    }@

    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