PDA

View Full Version : QSelectionModel and QSortFilterProxyModel



anbu01
17th April 2016, 12:38
Hi,
I am having test application that uses a tableview with model as CustomProxyModel (inherits from QSortFilterProxyModel) and the customproxymodel has a source model as StandardItemModel(the model has some data).When i tried to read the selected index using the following code:-


tableview->selectionModel()->selectedIndexs()

Everything works fine but when i change my selection behaviour to QAbstractItemView::SelectRows then crash happens.Am i missing some reimplementation methods from sortfilterproxymodel.

anda_skoa
17th April 2016, 13:27
What happens when the program crashes? What is the stack trace?

What kind of customization does you proxy model do on top of the QSortFilterProxyModel?

Cheers,
_

anbu01
17th April 2016, 14:01
I have attached the stack trace and customization of proxy is depending on the user optionwhich is done by reimplementing methods like :-


int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
QVariant data(const QModelIndex &index, int role) const;
Qt::ItemFlags flags(const QModelIndex &index) const;
QModelIndex mapToSource(const QModelIndex &proxyIndex) const;
QModelIndex mapFromSource(const QModelIndex &sourceIndex) const;
QModelIndex index(int row, int column, const QModelIndex &parent) const;
QModelIndex parent(const QModelIndex &child) const;

11896
11897

anda_skoa
17th April 2016, 14:18
Wow, that is quite a lot of override for a sort filter proxy customization.

I would have only expected overrides on lessThan() or filterAccepts...()
Interestingly those are not overwritten.

Looks like you have a fully custom proxy model, so I would check index+parent for correctness, as well as the map functions.
Especially parent() can be tricky.

Given that you are using a table view, are you sure you need custom implementation for methods relevant to a tree model?

Cheers,
_

anbu01
17th April 2016, 14:31
@ando_skoa:
As this is going to be generic proxy which can be for table or tree view so i need those functions.As you can see below are the implementation for the functions.I am very much confusd why selection model behaves like this when i change the behaviour(SelectRows).


QModelIndex LiPivotTableProxyModel::mapToSource(const QModelIndex &proxyIndex) const
{
return (sourceModel() && proxyIndex.isValid()) ? sourceModel()->index(proxyIndex.row(), proxyIndex.column(), proxyIndex.parent()) : QModelIndex();
}

QModelIndex LiPivotTableProxyModel::mapFromSource(const QModelIndex &sourceIndex) const
{
return index(sourceIndex.row(), sourceIndex.column(), sourceIndex.parent());
}
QModelIndex LiPivotTableProxyModel::index(int row, int column, const QModelIndex &parent) const
{
Q_UNUSED(parent);
return createIndex(row, column, row);
}

QModelIndex LiPivotTableProxyModel::parent(const QModelIndex &child) const
{
Q_UNUSED(child);
return QModelIndex();
}

As you can see parent i return new QModelIndex for my use case.

anda_skoa
17th April 2016, 15:32
Since index() and parent() don't do anything yet, you might want to leave their default implementations.

Your map functions are clearly wrong, since you are calling the respective index() method with a "parent" that doesn't belong to the model.

The implementations also don't look like they do any structural change, so am wondering why you override them at all.

Cheers,
_