zoukankan      html  css  js  c++  java
  • 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));
                    }
                }
            }
        }
  • 相关阅读:
    【算法】 冒泡排序
    【算法】 插入排序
    【算法】 斐波那契数列
    【C#】 RBAC 权限框架
    【jQuery】 实用 js
    【jQuery】 Ajax
    【jQuery】 常用函数
    【jQuery】 资料
    【jQuery】 效果
    Linaro/Yocto/Openwrt
  • 原文地址:https://www.cnblogs.com/chechen/p/15703852.html
Copyright © 2011-2022 走看看