<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Adjusting the selection behaviour of QStandardItem with QStyledItemDelegate]]></title><description><![CDATA[<p dir="auto">I am using QStyledItemDelegate to style the items in my QTreeView.</p>
<p dir="auto">The roots of my treeview are not decorated. It's just a simple tree with relation similar to the one below:</p>
<p dir="auto">@ColorBook1<br />
Color1<br />
Color2<br />
ColorBook2<br />
Color3@</p>
<p dir="auto">The parent and child are styled differently and selection on parent is disabled.</p>
<p dir="auto">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.</p>
<p dir="auto">Current Behaviour:<br />
!<a href="http://i.stack.imgur.com/IZvr2.png" target="_blank" rel="noopener noreferrer nofollow ugc">http://i.stack.imgur.com/IZvr2.png</a>(Curretn Behaviour)!</p>
<p dir="auto">Desired Behaviour:<br />
!<a href="http://i.stack.imgur.com/TCcM3.png" target="_blank" rel="noopener noreferrer nofollow ugc">http://i.stack.imgur.com/TCcM3.png</a>(Desired Behaviour)!</p>
<p dir="auto">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)?</p>
<p dir="auto">I am using the following code in the QStyledItemDelegate::paint method:</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/void">@<bdi>void</bdi></a> paint( QPainter * inPainter, const  QStyleOptionViewItem &amp; inOption, const QModelIndex &amp; inIndex ) const<br />
{<br />
if( inIndex.data( Qt::UserRole ) == ColorInfoType::kColorBook )<br />
{<br />
QFont font = inPainter-&gt;font();<br />
font.setWeight( QFont::Bold );<br />
font.setPointSize( 8 );<br />
inPainter-&gt;setFont( font );<br />
inPainter-&gt;drawText<br />
(<br />
inOption.rect.adjusted( 5,0,0,0 ),<br />
inIndex.data( Qt::DisplayRole ).toString(),<br />
QTextOption( Qt::AlignVCenter | Qt::AlignLeft )<br />
);<br />
}<br />
else<br />
{<br />
//To Do: draw the selection rect after adjusting the size.<br />
// Draw the Color Name text<br />
QStyledItemDelegate::paint( inPainter, inOption, inIndex );<br />
}<br />
}@</p>
]]></description><link>https://forum.qt.io/topic/51160/adjusting-the-selection-behaviour-of-qstandarditem-with-qstyleditemdelegate</link><generator>RSS for Node</generator><lastBuildDate>Wed, 29 Apr 2026 17:48:38 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/51160.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 13 Feb 2015 05:02:08 GMT</pubDate><ttl>60</ttl></channel></rss>