Search code examples
qtqmlqt5qtquick2qtquickcontrols

How to access QML ListView delegate items from C++?


In Listview, I have popping up 100's of items using "delegate", assume that, listview already displayed populated value. Now I would like to extract already displayed values in QML List view from C++. How to achieve this? Note: I can not access datamodel directly, since I am doing filtration in delegate using hidden variable

        /*This is not working code, Please note,
        delegate will not display all model data.*/
        ListView
        {
        id:"listview"
           model:datamodel
           delegate:{
                      if(!hidden)
                      {
                        Text{        
                        text:value
                      }
                    }

        }


 //Can I access by using given approach?
 QObject * object =   m_qmlengine->rootObjects().at(0)->findChild<QObject* >("listview");

//Find objects
const QListObject& lists = object->children();

//0 to count maximum
//read the first property
QVarient value =  QQmlProperty::read(lists[0],"text");

Solution

  • You can search for a specific item in QML with the objectName property. Let's take a look at a simple QML file:

    //main.qml
    Window {
        width: 1024; height: 768; visible: true
        Rectangle {
            objectName: "testingItem"
            width: 200; height: 40; color: "green"
        }
    }
    

    And in C++, assume engine is the QQmlApplicationEngine that loads main.qml, we can easily find testingItem by searching the QObject tree from QML root item using QObject::findChild:

    //C++
    void printTestingItemColor()
    {
        auto rootObj = engine.rootObjects().at(0); //assume main.qml is loaded
        auto testingItem = rootObj->findChild<QQuickItem *>("testingItem");
        qDebug() << testingItem->property("color");
    }
    

    However, this method cannot find all items in QML since some items may not have QObject parent. For example, delegates in ListView or Repeater:

    ListView {
        objectName: "view"
        width: 200; height: 80
        model: ListModel { ListElement { colorRole: "green" } }
        delegate: Rectangle {
            objectName: "testingItem" //printTestingItemColor() cannot find this!!
            width: 50; height: 50
            color: colorRole
        }
    }
    

    For delegates in ListView, we have to search the visual child instead of the object child. ListView delegates are parented to ListView's contentItem. So in C++ we have to search for the ListView first (with QObject::findChild), and then search for delegates in contentItem using QQuickItem::childItems:

    //C++
    void UIControl::printTestingItemColorInListView()
    {
        auto view = m_rootObj->findChild<QQuickItem *>("view");
        auto contentItem = view->property("contentItem").value<QQuickItem *>();
        auto contentItemChildren = contentItem->childItems();
        for (auto childItem: contentItemChildren )
        {
            if (childItem->objectName() == "testingItem")
                qDebug() << childItem->property("color");
        }
    }