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. How to create a QML Item for a QML ListView in c++
QtWS25 Last Chance

How to create a QML Item for a QML ListView in c++

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 2 Posters 2.3k 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.
  • L Offline
    L Offline
    lichti
    wrote on last edited by
    #1

    Hi,

    I tried to create a new QML Item for a ListView from c++, which works fine with setParentItem I can put it where ever I want.
    But it doesn't work if I set a ListView as parent. I can see my new Item but it is not realy a part of the model so when create a second item it does not appear under the first.
    I also tried to put the new item into the model which shows nothing. After all I found some examples how to manage the model thru a QList<Object*> but the I have no idea how to tell the delegate to draw it.

    Here is a small example:

    MyItem.qml
    @Rectangle {
    width: 100
    height: 70
    color: "red"
    }
    @

    main.qml
    @Rectangle {
    width: 400
    height:400
    ListView {
    id:thelist
    anchors.fill: parent
    clip: true
    }
    }
    @

    inside a function on the c++ side I do:
    @
    QDeclarativeComponent component(ui->qmlView->engine(),QUrl::fromLocalFile("MyItem.qml"));
    QObject *object = component.create();
    QDeclarativeItem item = qobject_cast<QDeclarativeItem>(object);

    QObject* listobject = ui->qmlView->rootObject()->findChild<QObject*>("thelist");
    
    QDeclarativeItem *listitem = qobject_cast<QDeclarativeItem*>(listobject);
    item->setParentItem(listitem);
    

    @

    Can someone give me a hint what I have to do to put the new item into the model for the listview

    Thanks in advance,
    lichti

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

      You definitely should set a delegate, then a model. Model should not be a visual item - the delegate is used to draw all items in a model.

      You can use Column element instead of ListView if you want a bit more flexibility (at a price, of course, of loosing some features - but most can be mimicked with Flickable).

      (Z(:^

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lichti
        wrote on last edited by
        #3

        Hi sierdzio,

        thank you for your answer. I got it working with a model and a delegate.
        It takes a while that I understand fully how the delegate and model works together.
        I think for simple Items this is the way to go, but for unknown complex items coming from another source imho its a big overhead.

        Cheers,
        lichti

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

          Yes, ListView is intended for a list of items that look and behave roughly the same. It's optimised towards that use case.

          This is why I proposed using Column as an alternative - it's more flexible, but also requires a bit more tinkering.

          (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