QTableView, QAbstractTableModel, QSortFilterProxyModel, Sorting
-
@Slawomir_Piernikowski said in QTableView, QAbstractTableModel, QSortFilterProxyModel, Sorting:
In qt doc is written that the sort function in QSortFilterProxyModel is derived from QAbstractItemModel class and the base class implementation does nothing.
But
QSortFilterProxyModel
reimplements it to do stuff.
It's easy to check the difference in code between QAbstractItemModel::sort and QSortFilterProxyModel::sort -
In Qt code:
void QAbstractItemModel::sort(int column, Qt::SortOrder order) { Q_UNUSED(column); Q_UNUSED(order); // do nothing }
void QSortFilterProxyModel::sort(int column, Qt::SortOrder order) { Q_D(QSortFilterProxyModel); if (d->dynamic_sortfilter && d->proxy_sort_column == column && d->sort_order == order) return; d->sort_order = order; d->proxy_sort_column = column; d->update_source_sort_column(); d->sort(); }
So as I see in the abowe code: if I have customized QSortFilterProxyModel the sort function is already overrided and do the sorting.... but
do You agree that if ui->tableView.setSortingEnabled(false); the sorting is not posible??? -
@Slawomir_Piernikowski said in QTableView, QAbstractTableModel, QSortFilterProxyModel, Sorting:
You agree that if ui->tableView.setSortingEnabled(false); the sorting is not posible???
No, I don't.
@VRonin said in QTableView, QAbstractTableModel, QSortFilterProxyModel, Sorting:
you are free to call ui->tableView->model()->sort(); wherever/whenever you want or even connect a signal to sort() of the model
-
So I decided to not use ui->tableView.setSortingEnabled(false) or ui->tableView.setSortingEnabled(true). I have customized proxy model and customized model which do not have overloaded sort functions.
I have tried to call setData function of the model(QAbstractTableModel) by clicking on a cell of the QTableView The data was changed but the model and view was not sorted ... so from the soft behaviour it looks like if you want to sort data from the level of the TableView it is obligatory to have set: ui->tableView.setSortingEnabled(true) or ui->tableView.setSortingEnabled(false) and use
sortByColumn function.
Am I right or I have missed something???
Maybe this will be helpful:void QTableView::sortByColumn(int column, Qt::SortOrder order) { Q_D(QTableView); if (column < 0) return; // If sorting is enabled it will emit a signal connected to // _q_sortIndicatorChanged, which then actually sorts d->horizontalHeader->setSortIndicator(column, order); // If sorting is not enabled, force to sort now if (!d->sortingEnabled) d->model->sort(column, order); }
If sotring is not enabled, force to sort now???
So even if ui->tableView.setSortingEnabled(false) and we call sortByColumn the model will be sorted.
I asked:
You agree that if ui->tableView.setSortingEnabled(false); the sorting is not posible???
You answered:
No, I don't.
Yes You were right even if ui->tableView.setSortingEnabled(false) we can still sort a model using
sortByColumn function.But if ui->tableView.setSortingEnabled(false) which is by default and we do not use sortByColumn function in my soft sorting not works when I set data of a cell of the tableView.
-
@VRonin said in QTableView, QAbstractTableModel, QSortFilterProxyModel, Sorting:
you are free to call ui->tableView->model()->sort();
Yes of course it can be done.
I just add in:bool MagazynModel::setData(const QModelIndex &index, const QVariant &value, int role) { .... this->sort(index.column()); }
and now when I set data in the tableView sorting is working.
-
@Slawomir_Piernikowski said in QTableView, QAbstractTableModel, QSortFilterProxyModel, Sorting:
when I set data of a cell of the tableView.
This was they key piece of information you left out in your previous posts. You want automatic sorting when data changes. In that case setting
ui->tableView->setSortingEnabled(true);
is the most convenient way, alternatively you canQObject::connect(ui->tableView->model(),&QAbstractItemModel::dataChanged,ui->tableView->model(),&QAbstractItemModel::sort,Qt::QueuedConnection);
P.S.
sortByColumn
has been deprecated, it should not be used in any new code -
QObject::connect(ui->tableView->model(),&QAbstractItemModel::dataChanged,ui->tableView->model(),&QAbstractItemModel::sort,Qt::QueuedConnection);
This is not the place for detailed discussion, but in a word why do you specify
Qt::QueuedConnection
here, please? -
Code from Qt doc:
void QTableView::sortByColumn(int column, Qt::SortOrder order) { Q_D(QTableView); if (column < 0) return; // If sorting is enabled it will emit a signal connected to // _q_sortIndicatorChanged, which then actually sorts d->horizontalHeader->setSortIndicator(column, order); // If sorting is not enabled, force to sort now if (!d->sortingEnabled) d->model->sort(column, order); // this sort function is called from //QAbstractItemModel so do nothing. See hereunder function }
/*! Sorts the model by \a column in the given \a order. The base class implementation does nothing. */ void QAbstractItemModel::sort(int column, Qt::SortOrder order) { Q_UNUSED(column); Q_UNUSED(order); // do nothing }
In Qt doc:
-
QTableView::setSortingEnabled(true) calls void QTableView::sortByColumn(int column, Qt::SortOrder order)
-
QTableView::sortByColumn fuction If sorting is enabled it will emit a signal connected to
_q_sortIndicatorChanged, which then actually sorts or If sorting is not enabled, force to sort now but if only the sort function in QAbstractItemModel::sort is overloaded and the body of the function has a code which sort, or you have customized QSortFilterProxyModel which has the sort function and the function is called when you click the header column arrow but not sorts when you setData from tableView level. -
if QTableView::setSortingEnabled(false) the header column arrow for descending or ascending sorting is not seen so there is no posibility to sort from the level of TableView.
-