Adjusting the selection behaviour of QStandardItem with QStyledItemDelegate

  • I am using QStyledItemDelegate to style the items in my QTreeView.

    The roots of my treeview are not decorated. It's just a simple tree with relation similar to the one below:


    The parent and child are styled differently and selection on parent is disabled.

    I want to customise the selection behaviour in the child nodes so that the selection rectangle around the child would cover the entire row and not the text child alone.

    Current Behaviour:
    ! Behaviour)!

    Desired Behaviour:
    ! Behaviour)!

    Is there any way to extend the selection rectangle like this using QStyledItemDelegate? I tried adjusting the rect in QStyleOptionViewItem parameter of QStyledItemDelegate::paint. But that moved the child node text to the left. I want to keep the text node at the same place but only the selection rectangle has to be adjusted to the left. So just like drawing text and pixmaps in the paint method is there a way to draw the selection rectangle as well(, using the default selection rect color)?

    I am using the following code in the QStyledItemDelegate::paint method:

    @void paint( QPainter * inPainter, const QStyleOptionViewItem & inOption, const QModelIndex & inIndex ) const
    if( Qt::UserRole ) == ColorInfoType::kColorBook )
    QFont font = inPainter->font();
    font.setWeight( QFont::Bold );
    font.setPointSize( 8 );
    inPainter->setFont( font );
    inOption.rect.adjusted( 5,0,0,0 ), Qt::DisplayRole ).toString(),
    QTextOption( Qt::AlignVCenter | Qt::AlignLeft )
    //To Do: draw the selection rect after adjusting the size.
    // Draw the Color Name text
    QStyledItemDelegate::paint( inPainter, inOption, inIndex );

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.