student
student

Reputation: 380

QSortFilterProxyModel in openPersistentEditor

I am trying to add a filtering and a sorting method to a tableView and for that I need to use QSortFilterProxyModel. My problem is that the initial model on witch I use the QSortFilterProxyModel needs that all the cells of the table are already open in editor mod. After I add the QStandardItemModel to the QSortFilterProxyModel the cells are not already in editable mode.

This is working as expected:

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));
}

The cells are displayed only if I double click on the cells. That means the openPersistestentEditor method of the tableView is not working properly.

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));
}

Upvotes: 1

Views: 820

Answers (1)

oranja
oranja

Reputation: 302

I just had this issue and just reading your question and code excerpt made me realize the mistake:

The view (ui->tableView) is set to work with one model (m_proxyModel), but the index for the editor comes from a different model (model). This probably doesn't make any sense to the view.1

Changing:

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

to:

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

should take care of your problem, I believe.

For me it created a different problem with the editor not displaying in the right cell, but it's probably something wrong with my subclass implementation of QAbstractProxyModel (which is generally not recommended).


1 I took a short glimpse at the source code for QAbstractItemView, and I didn't find an explicit restriction yet, but that's still the most reasonable explanation I can think of.

Upvotes: 3

Related Questions