diff --git a/src/widgets/itemviews/qabstractitemview.cpp b/src/widgets/itemviews/qabstractitemview.cpp
index a07297863d..da5119f7df 100644
--- a/src/widgets/itemviews/qabstractitemview.cpp
+++ b/src/widgets/itemviews/qabstractitemview.cpp
@@ -3060,6 +3060,9 @@ QSize QAbstractItemView::sizeHintForIndex(const QModelIndex &index) const
     Q_D(const QAbstractItemView);
     if (!d->isIndexValid(index) || !d->itemDelegate)
         return QSize();
+
+    QStyleOptionViewItem option = d->viewOptionsV1();
+    option.state.setFlag(QStyle::State_Selected, selectionModel()->isSelected(index));
     return d->delegateForIndex(index)->sizeHint(d->viewOptionsV1(), index);
 }
 
@@ -3088,6 +3091,8 @@ int QAbstractItemView::sizeHintForRow(int row) const
 
     ensurePolished();
 
+    const QModelIndexList selected = selectionModel()->selectedColumns(row);
+    int selectedFoundCount = 0;
     QStyleOptionViewItem option = d->viewOptionsV1();
     int height = 0;
     int colCount = d->model->columnCount(d->root);
@@ -3095,8 +3100,17 @@ int QAbstractItemView::sizeHintForRow(int row) const
         const QModelIndex index = d->model->index(row, c, d->root);
         if (QWidget *editor = d->editorForIndex(index).widget.data())
             height = qMax(height, editor->height());
-        if (const QAbstractItemDelegate *delegate = d->delegateForIndex(index))
+        if (const QAbstractItemDelegate *delegate = d->delegateForIndex(index)) {
+            const bool bSelected = false;
+            // one possible optimization for large dataset with small amount of selected items
+            if (selectedFoundCount < selected.size()) {
+                bSelected = selected.contains(index);
+                if (bSelected)
+                    ++selectedFoundCount;
+            }
+            option.state.setFlag(QStyle::State_Selected, bSelected);
             height = qMax(height, delegate->sizeHint(option, index).height());
+        }
     }
     return height;
 }
@@ -3118,6 +3132,7 @@ int QAbstractItemView::sizeHintForColumn(int column) const
 
     ensurePolished();
 
+    const QModelIndexList selected = selectionModel()->selectedRows(column);
     QStyleOptionViewItem option = d->viewOptionsV1();
     int width = 0;
     int rows = d->model->rowCount(d->root);
@@ -3126,7 +3141,10 @@ int QAbstractItemView::sizeHintForColumn(int column) const
         if (QWidget *editor = d->editorForIndex(index).widget.data())
             width = qMax(width, editor->sizeHint().width());
         if (const QAbstractItemDelegate *delegate = d->delegateForIndex(index))
+        {
+            option.state.setFlag(QStyle::State_Selected, selected.contains(index));
             width = qMax(width, delegate->sizeHint(option, index).width());
+        }
     }
     return width;
 }
diff --git a/src/widgets/itemviews/qtableview.cpp b/src/widgets/itemviews/qtableview.cpp
index f1ef92bdf8..85af4eb274 100644
--- a/src/widgets/itemviews/qtableview.cpp
+++ b/src/widgets/itemviews/qtableview.cpp
@@ -2269,6 +2269,7 @@ int QTableView::sizeHintForRow(int row) const
         if (d->horizontalHeader->isSectionHidden(logicalColumn))
             continue;
         index = d->model->index(row, logicalColumn, d->root);
+        option.state.setFlag(QStyle::State_Selected, selectionModel()->isSelected(index));
         hint = d->heightHintForIndex(index, hint, option);
 
         ++columnsProcessed;
@@ -2309,6 +2310,7 @@ int QTableView::sizeHintForRow(int row) const
             continue;
 
         index = d->model->index(row, logicalIdx, d->root);
+        option.state.setFlag(QStyle::State_Selected, selectionModel()->isSelected(index));
         hint = d->heightHintForIndex(index, hint, option);
         ++columnsProcessed;
     }
@@ -2358,6 +2360,7 @@ int QTableView::sizeHintForColumn(int column) const
             continue;
         index = d->model->index(logicalRow, column, d->root);
 
+        option.state.setFlag(QStyle::State_Selected, selectionModel()->isSelected(index));
         hint = d->widthHintForIndex(index, hint, option);
         ++rowsProcessed;
         if (rowsProcessed == maximumProcessRows)
