• QItemSelectionModel获取QModelIndexList程序崩溃


    工作中没有小事:点石成金,滴水成河,只有认真对待自己所做的一切事情,才能克服万难,取得成功。

    转载:https://blog.csdn.net/ljqiankun/article/details/50970423

    在项目中使用QT的QItemSelectionModel获取QModelIndexList时程序崩溃。

    使用场景:

    QItemSelectionModel *selections =pTreeView->selectionModel();
    QModelIndexList selectedindexes = selections->selectedRows();

    这样调用后就会报错。

    解决方法:找到selectedRows的源码,自己实现当前选中项

        QItemSelectionModel *selections = pTreeView->selectionModel();
    
        QModelIndexList selectedindexes;
        //the QSet contains pairs of parent modelIndex
        //and row number
        QSet<QPair<QModelIndex, int>> rowsSeen;
    
        const QItemSelection ranges = selections->selection();
        for (int i = 0; i < ranges.count(); ++i)
        {
            const QItemSelectionRange& range = ranges.at(i);
            QModelIndex parent = range.parent();
            for (int row = 0; i < range.top(); row <= range.bottom(); row++)
            {
                QPair<QModelIndex, int> rowDef = qMakePair(parent, row);
                if (!rowsSeen.contains(rowDef))
                {
                    rowsSeen << rowDef;
                    if (selections->isRowSelected(row, parent))
                    {
                        selectedindexes.append(pModel->index(row, 0, parent));
                    }
                }
            }
        }
  • 相关阅读:
    uni-app-数据缓存
    uni-app-网络请求
    uni-app-上拉加载
    uni-app-下拉刷新
    uni-app-生命周期
    uni-app字体图标
    uni-app-样式
    [Python] ValueError: Unknown resampling filter
    [Python]列表复制以及切片[:][::]解析
    LeetCode 29. 两数相除
  • 原文地址:https://www.cnblogs.com/chechen/p/15703852.html
Copyright © 2020-2023  润新知