QSortFilterProxyModel in openPersistentEditor

QSortFilterProxyModel in openPersistentEditor

本文关键字:openPersistentEditor in QSortFilterProxyModel      更新时间:2023-10-16

我试图为tableView添加过滤和排序方法,为此我需要使用QSortFilterProxyModel。我的问题是,我使用QSortFilterProxyModel的初始模型需要表的所有单元格已经在编辑器模式中打开。在我将QStandardItemModel添加到QSortFilterProxyModel后,单元格还没有处于可编辑模式。

运行正常:

QStandardItemModel *model = new QStandardItemModel(0, 5, this); //reimplemented class
QItemDelegate *mydelegate = new QItemDelegate(this); //reimplemented class
ui -> tableView -> setModel(model);
ui -> tableView -> setItemDelegate (mydelegate);
for(size_t i=0; i<m_BoardingsVector.size(); i++) //a structure from a function that adds rows dynamically
{
        model -> insertRows(model -> rowCount(),1);
        for(int j=0; j<5; ++j)
        ui -> tableView -> openPersistentEditor(model -> index(model -> rowCount() - 1, j));
}

单元格只有在双击单元格时才会显示。这意味着tableViewopenPersistestentEditor方法不能正常工作。

QStandardItemModel *model = new QStandardItemModel(0, 5, this); //reimplemented class
QItemDelegate *mydelegate = new QItemDelegate(this); //reimplemented class
QSortFilterProxyModel * m_proxyModel = new QSortFilterProxyModel();
m_proxyModel -> setSourceModel(model);
ui -> tableView -> setModel( m_proxyModel);
ui -> tableView -> setItemDelegate (mydelegate);
ui -> tableView -> sortByColumn(0, Qt::AscendingOrder);
ui -> tableView -> setSortingEnabled(true);

for(size_t i=0; i<m_BoardingsVector.size(); i++) //a structure from a function that adds rows dynamically
{
        model -> insertRows(model -> rowCount(),1);
        for(int j=0; j<5; ++j)
        ui -> tableView -> openPersistentEditor(model -> index(model -> rowCount() - 1, j));
}

我刚刚遇到这个问题,只是阅读你的问题和代码摘录让我意识到这个错误:

视图(ui->tableView)被设置为使用一个模型(m_proxyModel),但是编辑器的索引来自另一个模型(model)。1

改变:

ui -> tableView -> openPersistentEditor(model -> index(model -> rowCount() - 1, j));

:

ui -> tableView -> openPersistentEditor(m_proxyModel -> index(model -> rowCount() - 1, j));
我相信我能解决你的问题。

对我来说,它创建了一个不同的问题,编辑器没有显示在正确的单元格中,但这可能是我的QAbstractProxyModel子类实现的问题(通常不推荐)。


<我>1我瞥了一眼QAbstractItemView的源代码,我还没有找到明确的限制,但这仍然是我能想到的最合理的解释。

相关文章:
  • 没有找到相关文章