QStandardItemModel 遍历勾选的项

QStandardItemModel 遍历勾选的项

rowCount()不能传入 m_model->index(0, 0)根节点,无法获取行数;

不传,或者传入一个空QModelIndex对象,可以获取到第一级节点的数量;

QMap<QString,QVariantMap> mapSelectVideo;
    int rootRowCount = m_model->rowCount();
    for (int i= 0;i<rootRowCount;i++)
    {
        QModelIndex rootIndex = m_model->index(i, 0);
        RecursiveFindCheckedVideo(rootIndex, mapSelectVideo);
    }

 

void SelectVideoTree::RecursiveFindCheckedVideo(QModelIndex& parent, QMap<QString, QVariantMap>& mapSelectFile)
{
    if (parent.isValid())
    {
        int rowCount = m_model->rowCount(parent);
        for (int i = 0; i < rowCount; ++i)
        {
            QModelIndex childIndex = m_model->index(i, 0, parent);
            QStandardItem* pItem = m_model->itemFromIndex(childIndex);
            if (pItem)
            {
                QVariantMap info = pItem->data().toMap();
                if (info.value("type") == "2")//视频文件
                {
                    mapSelectFile.insert(info.value("location").toString(), info);
                }
                else
                {
                    RecursiveFindCheckedVideo(childIndex, mapSelectFile); // 递归遍历子项
                }
            }

        }
    }
}
posted @ 2024-05-16 20:15  一字千金  阅读(57)  评论(0编辑  收藏  举报