summaryrefslogtreecommitdiffstats
path: root/src/widgets/itemviews/qtableview.cpp
diff options
context:
space:
mode:
authorChristian Ehrlicher <ch.ehrlicher@gmx.de>2018-10-26 20:38:58 +0200
committerShawn Rutledge <shawn.rutledge@qt.io>2018-11-02 05:56:58 +0000
commitd0f909f8dbdd8594b0d950822f0e7ab8728da513 (patch)
treee1d3b59a5eb94559a963cd0c664d4b8498336fe3 /src/widgets/itemviews/qtableview.cpp
parentd84a7a812818065f97e015e50f66f6dad8c9a978 (diff)
QTreeView/TableView: explicitly mark sortByColumn(int) as deprecated
QTreeView/TableView::sortByColumn(int) was deprecated a long time ago but never got removed. Therefore mark it with QT_DEPRECATED_SINCE(5, 13) so we can remove it with Qt6. Also sync the handling of the sort order changes in QTableView with the one from QTreeView. Change-Id: I0371d9a9c21116edaa9125835827f1a200075d36 Reviewed-by: Luca Beldi <v.ronin@yahoo.it> Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
Diffstat (limited to 'src/widgets/itemviews/qtableview.cpp')
-rw-r--r--src/widgets/itemviews/qtableview.cpp35
1 files changed, 27 insertions, 8 deletions
diff --git a/src/widgets/itemviews/qtableview.cpp b/src/widgets/itemviews/qtableview.cpp
index 32bbef07e9..dac8174a2a 100644
--- a/src/widgets/itemviews/qtableview.cpp
+++ b/src/widgets/itemviews/qtableview.cpp
@@ -904,6 +904,15 @@ void QTableViewPrivate::_q_updateSpanRemovedColumns(const QModelIndex &parent, i
/*!
\internal
+ Sort the model when the header sort indicator changed
+*/
+void QTableViewPrivate::_q_sortIndicatorChanged(int column, Qt::SortOrder order)
+{
+ model->sort(column, order);
+}
+
+/*!
+ \internal
Draws a table cell.
*/
void QTableViewPrivate::drawCell(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index)
@@ -2573,25 +2582,27 @@ void QTableView::setColumnHidden(int column, bool hide)
void QTableView::setSortingEnabled(bool enable)
{
Q_D(QTableView);
- d->sortingEnabled = enable;
horizontalHeader()->setSortIndicatorShown(enable);
if (enable) {
disconnect(d->horizontalHeader, SIGNAL(sectionEntered(int)),
this, SLOT(_q_selectColumn(int)));
disconnect(horizontalHeader(), SIGNAL(sectionPressed(int)),
this, SLOT(selectColumn(int)));
- connect(horizontalHeader(), SIGNAL(sortIndicatorChanged(int,Qt::SortOrder)),
- this, SLOT(sortByColumn(int)), Qt::UniqueConnection);
+ //sortByColumn has to be called before we connect or set the sortingEnabled flag
+ // because otherwise it will not call sort on the model.
sortByColumn(horizontalHeader()->sortIndicatorSection(),
horizontalHeader()->sortIndicatorOrder());
+ connect(horizontalHeader(), SIGNAL(sortIndicatorChanged(int,Qt::SortOrder)),
+ this, SLOT(_q_sortIndicatorChanged(int,Qt::SortOrder)), Qt::UniqueConnection);
} else {
connect(d->horizontalHeader, SIGNAL(sectionEntered(int)),
this, SLOT(_q_selectColumn(int)), Qt::UniqueConnection);
connect(horizontalHeader(), SIGNAL(sectionPressed(int)),
this, SLOT(selectColumn(int)), Qt::UniqueConnection);
disconnect(horizontalHeader(), SIGNAL(sortIndicatorChanged(int,Qt::SortOrder)),
- this, SLOT(sortByColumn(int)));
+ this, SLOT(_q_sortIndicatorChanged(int,Qt::SortOrder)));
}
+ d->sortingEnabled = enable;
}
bool QTableView::isSortingEnabled() const
@@ -3120,19 +3131,21 @@ void QTableView::resizeColumnsToContents()
d->horizontalHeader->resizeSections(QHeaderView::ResizeToContents);
}
+#if QT_DEPRECATED_SINCE(5, 13)
/*!
\obsolete
\overload
+ This function is deprecated. Use
+ sortByColumn(int column, Qt::SortOrder order) instead.
Sorts the model by the values in the given \a column.
*/
void QTableView::sortByColumn(int column)
{
Q_D(QTableView);
- if (column == -1)
- return;
- d->model->sort(column, d->horizontalHeader->sortIndicatorOrder());
+ sortByColumn(column, d->horizontalHeader->sortIndicatorOrder());
}
+#endif
/*!
\since 4.2
@@ -3144,8 +3157,14 @@ void QTableView::sortByColumn(int column)
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);
- sortByColumn(column);
+ // If sorting is not enabled, force to sort now
+ if (!d->sortingEnabled)
+ d->model->sort(column, order);
}
/*!