Along with setting uniformRowHeights
off in in your QTreeView
here is what I would try.
There are few ways to do this, I like to use Qt's signals/slots, as such we're going to change the height through a custom QAbstractItemModel
on the QTreeView
. This custom model will be connected to the signal selectionChanged
from the QItemSelectionModel
of your QTreeView
. The example code/snippets work with single selection mode but you can easily alter it to handle multiple selected rows.
Step 1 - Create Custom Model with Selection slot
Create the custom model class that derives from QAbstractItemModel
and make sure you create a slot such as:
Q_SLOTS:
void onSelectionChanged( const QItemSelection&, const QItemSelection& );
Inside your model class add the following snippets/methods.
void MyModelClass::onSelectionChanged( const QItemSelection& selected,
const QItemSelection& deselected )
{
if( !selected.empty() )
{
// Save the index within the class.
m_selectedIndex = selected.first();
Q_EMIT dataChanged( m_selectedIndex, m_selectedIndex );
}
}
QVariant MyModelClass::data( const QModelIndex& index, int role ) const
{
// Use the selected index received from the selection model.
if( m_selectedIndex.isValid() &&
index == m_selectedIndex &&
role == Qt::SizeHintRole )
{
// Return our custom size!
return QSize( 50, 50 );
}
...
}
Step 2 - Connect Selection Changes to Your Model
Inside the initialization of your QTreeView
create your custom model and do the following:
MyTreeView::MyTreeView( QWidget* parent ) : QWidget( parent )
{
...
MyModelClass* model = new MyModelClass();
setModel( model );
setSelectionMode( QAbstractItemView::SingleSelection );
setSelectionBehavior( QAbstractItemView::SelectRows );
connect
(
selectionModel(),
SIGNAL( selectionChanged(const QItemSelection&, const QItemSelection&) ),
model,
SLOT( onSelectionChanged(const QItemSelection&, const QItemSelection&) )
);
}
I'm sure there are a few ways of doing this, i.e. handing the QItemSelectionModel
directly to your QAbstractItemModel
but again I prefer to use signals/slots and to save the selection in the model.
Hope this helps.