diff options
Diffstat (limited to 'src/widgets/itemviews/qtableview.cpp')
-rw-r--r-- | src/widgets/itemviews/qtableview.cpp | 41 |
1 files changed, 29 insertions, 12 deletions
diff --git a/src/widgets/itemviews/qtableview.cpp b/src/widgets/itemviews/qtableview.cpp index 9725a768de..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) @@ -1111,8 +1120,6 @@ QSize QTableView::viewportSizeHint() const Q_D(const QTableView); QSize result( (d->verticalHeader->isHidden() ? 0 : d->verticalHeader->width()) + d->horizontalHeader->length(), (d->horizontalHeader->isHidden() ? 0 : d->horizontalHeader->height()) + d->verticalHeader->length()); - result += QSize(verticalScrollBar()->isVisible() ? verticalScrollBar()->width() : 0, - horizontalScrollBar()->isVisible() ? horizontalScrollBar()->height() : 0); return result; } @@ -1383,12 +1390,12 @@ void QTableView::paintEvent(QPaintEvent *event) //firstVisualRow is the visual index of the first visible row. lastVisualRow is the visual index of the last visible Row. //same goes for ...VisualColumn int firstVisualRow = qMax(verticalHeader->visualIndexAt(0),0); - int lastVisualRow = verticalHeader->visualIndexAt(verticalHeader->viewport()->height()); + int lastVisualRow = verticalHeader->visualIndexAt(verticalHeader->height()); if (lastVisualRow == -1) lastVisualRow = d->model->rowCount(d->root) - 1; int firstVisualColumn = horizontalHeader->visualIndexAt(0); - int lastVisualColumn = horizontalHeader->visualIndexAt(horizontalHeader->viewport()->width()); + int lastVisualColumn = horizontalHeader->visualIndexAt(horizontalHeader->width()); if (rightToLeft) qSwap(firstVisualColumn, lastVisualColumn); if (firstVisualColumn == -1) @@ -2575,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 @@ -3122,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 @@ -3146,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); } /*! |