Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 子类化QItemSelectionModel C++/Qt_C++_Qt - Fatal编程技术网

C++ 子类化QItemSelectionModel C++/Qt

C++ 子类化QItemSelectionModel C++/Qt,c++,qt,C++,Qt,我需要子类QItemSelectionModel来处理我的特定模型的选择。 主要是出于性能原因,因为像QItemSelectionModel::selectedRows()这样的方法效率很低:它每次都从头开始构建选择,对其排序并从我的代理模型返回结果 对我来说最好的方法是覆盖QItemSelectionModel::emitSelectionChanged(const-QItemSelection&newSelection,const-QItemSelection&oldSelection) 但

我需要子类
QItemSelectionModel
来处理我的特定模型的选择。 主要是出于性能原因,因为像
QItemSelectionModel::selectedRows()
这样的方法效率很低:它每次都从头开始构建选择,对其排序并从我的代理模型返回结果

对我来说最好的方法是覆盖
QItemSelectionModel::emitSelectionChanged(const-QItemSelection&newSelection,const-QItemSelection&oldSelection)

但这不是一个虚拟的方法。 重载也不起作用(视图调用基本实现)

QItemSelectionModel::select(…)
是调用
emitSelectionChanged
的插槽,但是,即使它是虚拟的,它也使用带有d指针的私有数据,覆盖起来似乎很糟糕

有没有办法绕过这一混乱局面

目前,我正在使用此插槽连接到
selectionChanged(…)
信号:

void onSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
{
    selection.merge(selected, QItemSelectionModel::Select);
    selection.merge(deselected, QItemSelectionModel::Deselect);

    selectedRows.clear();

    if (!selection.isEmpty()) {
        const QModelIndexList &indexes = selection.indexes();
        int columns = proxy->columnCount();

        if (indexes.at(0).row() != indexes.at(1).row()) {
            // Shift selection
            int size = indexes.size() / columns;
            for (int i = 0; i < size; ++i) {
                selectedRows.append(proxy->mapToSource(indexes.at(i)));
            }

        } else {
            // Ctrl selection
            int size = indexes.size();
            for (int i = 0; i < size; i+=columns) {
                selectedRows.append(proxy->mapToSource(indexes.at(i)));
            }
        }
    }
}
void onselection已更改(const QItemSelection&selected,const QItemSelection&deselected)
{
selection.merge(selected,QItemSelectionModel::Select);
selection.merge(取消选中,QItemSelectionModel::取消选中);
selectedRows.clear();
如果(!selection.isEmpty()){
const QModelIndexList&index=selection.index();
int columns=proxy->columnCount();
if(index.at(0.row()!=index.at(1.row()){
//班次选择
int size=index.size()/列;
对于(int i=0;imapToSource(index.at(i));
}
}否则{
//Ctrl选择
int size=index.size();
for(int i=0;imapToSource(index.at(i));
}
}
}
}
如果需要,以下是来源:


谢谢。

很抱歉,覆盖它并不是我认为最好的包装方式。但我也不认为这样做能真正提高性能。