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. Using an instantiated item as ListView delegate
Forum Updated to NodeBB v4.3 + New Features

Using an instantiated item as ListView delegate

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 899 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.
  • H Offline
    H Offline
    hesmar
    wrote on last edited by
    #1

    In a QtQuick-ListView I want to display different QQuickItems which were instantiated in a C++-Backend. Therefore I created a QObjectList which is used as model:
    @
    QObjectList items;
    items.append(new MyFirstQuickItem());
    items.append(new MySecondQuickItem());

    QQmlContext *ctxt = viewer.rootContext();
    ctxt->setContextProperty("myModel", QVariant::fromValue(items));
    @
    In the ListView, I add the the items as a child of the delegate item:
    @
    ListView {
    id: view
    anchors.fill: parent
    model: myModel
    delegate: Item {
    id: delegateItem
    width: 100
    height: 50
    children: modelData
    }
    }
    @
    With this solution, it is possible to show the items in the list. However, the displayed items have their default size. Is it possible to change the size of the items to the size of the "delegateItem"?

    Thanks!

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jens
      wrote on last edited by
      #2

      If you make the delegate Item a Row or Column for instance, it will resize to the height of its child items. You can also try to bind the height property of the delegate to childrenRect.height.

      1 Reply Last reply
      0
      • H Offline
        H Offline
        hesmar
        wrote on last edited by
        #3

        The problem is, that I want to resize the child item to the size of the delegate item and not the delegate to the child. I tried this by calling the following method after I changed the model:
        @
        void QuickItemMngrServiceImpl::anchorItems()
        {
        QQmlContext *ctxt = m_viewer->rootContext();

        Q_FOREACH(QObject *item, m_items)
        {
            QQmlExpression expr(ctxt, item, "parent");
            QQmlProperty prop(item, "anchors.fill");
            prop.write(expr.evaluate());
        }
        

        }
        @
        But this works only for the items, that are visible at the beginning.

        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