4

I'm using Qt 4.7.0, a Qtreeview with multiple columns.

What I want to do is "simple" : I want a line to increase its height, when it's selected.

Will delegates be enough to do this ?

I've been through some stuff with a QTableView :

m_pMyTableView->verticalHeader()->setResizeMode(QHeaderView::Interactive);
...
QSize AbstractItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;

It's working with this tableview, but I can't see how I'll do this on a QTreeview, since, for a start, it doesn't have any vertical headers...

Can someone enlight my path please ?

Andy M
  • 5,945
  • 7
  • 51
  • 96
  • 2
    Remember to double-check your treeview and make sure that the uniformRowHeights option is false. Otherwise, it will likely only ask for the size for one index, and use that for all of them. (I have no other insight.) – Caleb Huitt - cjhuitt Oct 12 '10 at 15:27

1 Answers1

1

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.

Matthew
  • 2,759
  • 18
  • 28