Using an instantiated item as ListView delegate



  • 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!



  • 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.



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.