How to change delegate’s sizeHint on TableView’s cell focus?

639 Views Asked by At

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?

1

There are 1 best solutions below

0
On

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 the QItemSelectionModel.