如何从 C++ 访问 QML ListView 委托项?

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

Listview 中,我使用“delegate”弹出了 100 个项目,假设 listview 已经显示填充值。 现在我想从 C++ 中提取 QML 列表视图中已经显示的值。如何做到这一点? 笔记: 我无法直接访问数据模型, 因为我正在使用 隐藏 变量

在委托中进行过滤
        /*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");

您可以使用 objectName 属性 在 QML 中搜索特定项目。我们来看一个简单的QML文件:

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

而在 C++ 中,假设 engine 是加载 main.qml 的 QQmlApplicationEngine,我们可以通过使用 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");
}

但是,此方法无法找到 QML 中的所有项目,因为某些项目可能没有 QObject 父级。例如,ListViewRepeater 中的委托:

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

对于 ListView 中的代表,我们必须搜索 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");
    }
}