Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Adjusting the selection behaviour of QStandardItem with QStyledItemDelegate

Adjusting the selection behaviour of QStandardItem with QStyledItemDelegate

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 603 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SajasKK
    wrote on last edited by
    #1

    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:

    @ColorBook1
    Color1
    Color2
    ColorBook2
    Color3@

    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:
    !http://i.stack.imgur.com/IZvr2.png(Curretn Behaviour)!

    Desired Behaviour:
    !http://i.stack.imgur.com/TCcM3.png(Desired 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( inIndex.data( Qt::UserRole ) == ColorInfoType::kColorBook )
    {
    QFont font = inPainter->font();
    font.setWeight( QFont::Bold );
    font.setPointSize( 8 );
    inPainter->setFont( font );
    inPainter->drawText
    (
    inOption.rect.adjusted( 5,0,0,0 ),
    inIndex.data( Qt::DisplayRole ).toString(),
    QTextOption( Qt::AlignVCenter | Qt::AlignLeft )
    );
    }
    else
    {
    //To Do: draw the selection rect after adjusting the size.
    // Draw the Color Name text
    QStyledItemDelegate::paint( inPainter, inOption, inIndex );
    }
    }@

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved