diff options
author | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2018-04-27 16:08:22 +0200 |
---|---|---|
committer | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2018-05-09 11:16:46 +0000 |
commit | bbcf4193b35e3931205d32e87c0c3de9f68cc584 (patch) | |
tree | dc8bde115077ada1aac1a78a570f592b4576c95d /src/quick | |
parent | 5e9fc09f17d5a91f09423d6f41a4cd53266cffac (diff) |
TableView: ensure we check that returned iterators are not at the end
The current implementation would sometimes just crash because we
didn't check if the returned iterators were pointing to something
valid. This patch will fix this.
Change-Id: Ia45a8e3b701fb9067bf9116f39d7753b88f4f734
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/quick')
-rw-r--r-- | src/quick/items/qquicktableview.cpp | 60 |
1 files changed, 40 insertions, 20 deletions
diff --git a/src/quick/items/qquicktableview.cpp b/src/quick/items/qquicktableview.cpp index 2e9afbbae7..860db13561 100644 --- a/src/quick/items/qquicktableview.cpp +++ b/src/quick/items/qquicktableview.cpp @@ -617,21 +617,31 @@ void QQuickTableViewPrivate::calculateTableSize() qreal QQuickTableViewPrivate::columnWidth(int column) { if (!columnWidths.isEmpty()) { - // Find the ColumnRowSize assignment before, or at, column - auto iter = std::lower_bound(columnWidths.constBegin(), columnWidths.constEnd(), + // Find the first ColumnRowSize with a column before, or at, the given column + auto iter = std::upper_bound(columnWidths.constBegin(), columnWidths.constEnd(), ColumnRowSize{column, -1}, ColumnRowSize::lessThan); - // First check if we got an explicit assignment - if (iter->index == column) - return iter->size; - - // If the table is not a list, return the size of - // ColumnRowSize element found before column. - if (tableSize.height() > 1) - return (iter - 1)->size; + if (iter == columnWidths.constEnd()) { + // If the table is not a list, return the size + // of the last recorded ColumnRowSize. + if (tableSize.height() > 1) + return columnWidths.last().size; + } else { + // Check if we got an explicit assignment for this column + if (iter->index == column) + return iter->size; + + // If the table is not a list, return the size of + // ColumnRowSize element found before column. Since there + // is always an element stored for column 0, this is safe. + // Otherwise we continue, and return the size of the delegate + // item at the given column instead. + if (tableSize.height() > 1) + return (iter - 1)->size; + } } - // if we have an item loaded at column, return the width of the item. + // If we have an item loaded at column, return the width of the item. if (column >= loadedTable.left() && column <= loadedTable.right()) return loadedTableItem(QPoint(column, 0))->geometry().width(); @@ -645,17 +655,27 @@ qreal QQuickTableViewPrivate::rowHeight(int row) auto iter = std::lower_bound(rowHeights.constBegin(), rowHeights.constEnd(), ColumnRowSize{row, -1}, ColumnRowSize::lessThan); - // First check if we got an explicit assignment - if (iter->index == row) - return iter->size; - - // If the table is not a list, return the size of - // ColumnRowSize element found before column. - if (q_func()->columns() > 1) - return (iter - 1)->size; + if (iter == rowHeights.constEnd()) { + // If the table is not a list, return the size + // of the last recorded ColumnRowSize. + if (tableSize.width() > 1) + return rowHeights.last().size; + } else { + // Check if we got an explicit assignment for this row + if (iter->index == row) + return iter->size; + + // If the table is not a list, return the size of + // ColumnRowSize element found before row. Since there + // is always an element stored for row 0, this is safe. + // Otherwise we continue, and return the size of the delegate + // item at the given row instead. + if (tableSize.width() > 1) + return (iter - 1)->size; + } } - // if we have an item loaded at row, return the height of the item. + // If we have an item loaded at row, return the height of the item. if (row >= loadedTable.top() && row <= loadedTable.bottom()) return loadedTableItem(QPoint(0, row))->geometry().height(); |