I've got some troubles with custom delegate's sizeHint: I want to "unfold" cell (row with this cell) on it's focus; otherwise return default size; QTableVew connected to resize rows on mouse press:
connect(m_scheduleView, &QTableView::pressed, m_scheduleView, &QTableView::resizeRowsToContents);
QSize DBScheduleItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (option.state & QStyle::State_HasFocus) {
... // this block never executes;
return // some calculated size;
}
return QSize(width, height); // default size;
}
Code in conditional block has never executed, but the same condition on delegate's paint() executes properly:
void DBScheduleItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (option.state & QStyle::State_HasFocus)
painter->fillRect(...);
}
So, how to catch cell focus in sizeHint?
This is related to https://bugreports.qt.io/browse/QTBUG-5392 .
A possible work-around that is also used in the source code of Qt Creator itself (specifically in the
TaskView
widget) is to keep track of the current/selected item(s) yourself by connecting to theQItemSelectionModel
.