<?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[QStyledItemDelegate &amp; QRect]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">I'm wirking on a music player and I have asked here before for a custom delegate for a QListView with two text displays:</p>
<p dir="auto">#ifndef ALBUMDELEGATE<br />
#define ALBUMDELEGATE<br />
#include &lt;QMainWindow&gt;<br />
#include &lt;QRect&gt;<br />
#include &lt;QStyledItemDelegate&gt;<br />
#include &lt;QPainter&gt;</p>
<p dir="auto">struct AlbumDelegate: public QStyledItemDelegate<br />
{<br />
AlbumDelegate(QObject* parent = NULL) : QStyledItemDelegate(parent) {}</p>
<pre><code>void paint(QPainter* p, const QStyleOptionViewItem&amp; o, const QModelIndex&amp; idx) const override
{
    // get the data from the index
    QString title  = idx.data(Qt::UserRole+2).toString();
    QString artist = idx.data(Qt::UserRole+3).toString();

    QRect titleRect  = o.rect.adjusted(0, o.rect.height()-40, o.rect.width(), o.rect.height()-20);
    QRect artistRect = o.rect.adjusted(0, o.rect.height()-20, o.rect.width(), o.rect.height());

    //draw the data in their rectangles
    p-&gt;drawText(titleRect, Qt::AlignLeft, title);
    p-&gt;drawText(artistRect, Qt::AlignLeft, artist);
}

QSize sizeHint(const QStyleOptionViewItem&amp; o, const QModelIndex&amp; idx) const override
{
    //get the default size hint
    QSize sh = QStyledItemDelegate::sizeHint(o, idx);
    //modify the hint to fit 2 lines of text
    return QSize(sh.width(), sh.height()*2);
}
</code></pre>
<p dir="auto">};</p>
<p dir="auto">#endif // ALBUMDELEGATE</p>
<p dir="auto">this is the answer I got (from chris kawa). but when I apply this delegate to my listVew the Icon of the item disappears. what can I do to fix it?</p>
]]></description><link>https://forum.qt.io/topic/63705/qstyleditemdelegate-qrect</link><generator>RSS for Node</generator><lastBuildDate>Fri, 01 May 2026 14:52:54 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/63705.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 02 Feb 2016 14:57:24 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QStyledItemDelegate &amp; QRect on Wed, 03 Feb 2016 18:03:38 GMT]]></title><description><![CDATA[<p dir="auto">Hi, thank you it worked. but I still have problem with the icon</p>
]]></description><link>https://forum.qt.io/post/311273</link><guid isPermaLink="true">https://forum.qt.io/post/311273</guid><dc:creator><![CDATA[shahriar25]]></dc:creator><pubDate>Wed, 03 Feb 2016 18:03:38 GMT</pubDate></item><item><title><![CDATA[Reply to QStyledItemDelegate &amp; QRect on Wed, 03 Feb 2016 06:35:26 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/shahriar25">@<bdi>shahriar25</bdi></a> said:</p>
<blockquote>
<p dir="auto">Hi, I was working on another delegate that doesn't need icon to learn and I saw another problem:<br />
the QListView with this delegate doesn't show the selected items. when you click on the item the signal slot that I set for it activates but the item doesn't get selected. what should I do about that?</p>
</blockquote>
<p dir="auto">Let Qt do the actual drawing of all the stuff except the icon and the text:</p>
<pre><code>void MyItemDelegate::paint(QPainter *painter,
        const QStyleOptionViewItem &amp;option, const QModelIndex &amp;index) const
{
    QStyleOptionViewItemV4 opt = option;
    initStyleOption(&amp;opt, index);
    opt.icon = QIcon();
    opt.text = QString();

    const QWidget *widget = option.widget;
    QStyle *style = widget ? widget-&gt;style() : QApplication::style();
    style-&gt;drawControl(QStyle::CE_ItemViewItem, &amp;opt, painter, widget);

    // draw icon and text
    ....
}
</code></pre>
]]></description><link>https://forum.qt.io/post/311151</link><guid isPermaLink="true">https://forum.qt.io/post/311151</guid><dc:creator><![CDATA[raven-worx]]></dc:creator><pubDate>Wed, 03 Feb 2016 06:35:26 GMT</pubDate></item><item><title><![CDATA[Reply to QStyledItemDelegate &amp; QRect on Wed, 03 Feb 2016 06:24:03 GMT]]></title><description><![CDATA[<p dir="auto">Hi, I was working on another delegate that doesn't need icon to learn and I saw another problem:<br />
the QListView with this delegate doesn't show the selected items. when you click on the item the signal slot that I set for it activates but the item doesn't get selected. what should I do about that?</p>
<p dir="auto">struct TrackDelegate: public QStyledItemDelegate<br />
{<br />
TrackDelegate(QObject* parent = NULL) : QStyledItemDelegate(parent) {}</p>
<pre><code>void paint(QPainter* p, const QStyleOptionViewItem&amp; o, const QModelIndex&amp; idx) const override
{
    // get the data from the index
    QString track  = idx.data(Qt::UserRole+2).toString();
    QString artist = idx.data(Qt::UserRole+6).toString();
    QString title = idx.data(Qt::UserRole+3).toString();

    QRect trackRect  = o.rect.adjusted(0, 0, 30, o.rect.height());
    QRect titleRect = o.rect.adjusted(40, 0, o.rect.width(), o.rect.height()/2);
    QRect artistRect = o.rect.adjusted(40, o.rect.height()/2, o.rect.width(), o.rect.height());

    //draw the data in their rectangles
    p-&gt;drawText(trackRect, Qt::AlignLeft, track);
    p-&gt;drawText(artistRect, Qt::AlignLeft, artist);
    p-&gt;drawText(titleRect, Qt::AlignLeft, title);
}

QSize sizeHint(const QStyleOptionViewItem&amp; o, const QModelIndex&amp; idx) const override
{
    //get the default size hint
    QSize sh = QStyledItemDelegate::sizeHint(o, idx);
    //modify the hint to fit 2 lines of text
    return QSize(sh.width(), sh.height());
}
</code></pre>
<p dir="auto">};</p>
]]></description><link>https://forum.qt.io/post/311146</link><guid isPermaLink="true">https://forum.qt.io/post/311146</guid><dc:creator><![CDATA[shahriar25]]></dc:creator><pubDate>Wed, 03 Feb 2016 06:24:03 GMT</pubDate></item><item><title><![CDATA[Reply to QStyledItemDelegate &amp; QRect on Tue, 02 Feb 2016 20:18:45 GMT]]></title><description><![CDATA[<p dir="auto">Hi, I tried to do that but I don't know much about QRecr an QPainter</p>
<p dir="auto">#ifndef ALBUMDELEGATE<br />
#define ALBUMDELEGATE</p>
<p dir="auto">#include &lt;QMainWindow&gt;<br />
#include &lt;QRect&gt;<br />
#include &lt;QStyledItemDelegate&gt;<br />
#include &lt;QPainter&gt;</p>
<p dir="auto">struct AlbumDelegate: public QStyledItemDelegate<br />
{<br />
AlbumDelegate(QObject* parent = NULL) : QStyledItemDelegate(parent) {}</p>
<pre><code>void paint(QPainter* p, const QStyleOptionViewItem&amp; o, const QModelIndex&amp; idx) const override
{
    // get the data from the index
    QString title  = idx.data(Qt::UserRole+2).toString();
    QString artist = idx.data(Qt::UserRole+3).toString();
    QIcon icon = idx.data(Qt::DecorationRole).value&lt;QIcon&gt;();

    //split cell rectangle into upper and lower half
    //QRect titleRect  = o.rect.adjusted(0, 0, 0, -o.rect.height()/2);
    //QRect artistRect = o.rect.adjusted(0, o.rect.height()/2, 0, 0);

    QRect iconRect = o.rect.adjusted(0, 0, o.rect.width(), o.rect.height()-40);
    QRect titleRect  = o.rect.adjusted(0, o.rect.height()-40, o.rect.width(), o.rect.height()-20);
    QRect artistRect = o.rect.adjusted(0, o.rect.height()-20, o.rect.width(), o.rect.height());

    //draw the data in their rectangles
    p-&gt;drawPixmap(iconRect, icon.pixmap(o.rect.width(), o.rect.width()),o.rect);
    p-&gt;drawText(titleRect, Qt::AlignLeft, title);
    p-&gt;drawText(artistRect, Qt::AlignLeft, artist);
}

QSize sizeHint(const QStyleOptionViewItem&amp; o, const QModelIndex&amp; idx) const override
{
    //get the default size hint
    QSize sh = QStyledItemDelegate::sizeHint(o, idx);
    //modify the hint to fit 2 lines of text
    return QSize(sh.width(), sh.height()*2);
}
</code></pre>
<p dir="auto">};</p>
<p dir="auto">#endif // ALBUMDELEGATE</p>
<p dir="auto">and also I have set shortcuts to resize icons of items by triggreing them. I want those to work properly.<br />
What should I do?</p>
]]></description><link>https://forum.qt.io/post/311071</link><guid isPermaLink="true">https://forum.qt.io/post/311071</guid><dc:creator><![CDATA[shahriar25]]></dc:creator><pubDate>Tue, 02 Feb 2016 20:18:45 GMT</pubDate></item><item><title><![CDATA[Reply to QStyledItemDelegate &amp; QRect on Tue, 02 Feb 2016 15:53:33 GMT]]></title><description><![CDATA[<p dir="auto">get the icon with <strong>idx.data(Qt::DecorationRole)</strong> and paint it on the appropriate place.</p>
]]></description><link>https://forum.qt.io/post/311044</link><guid isPermaLink="true">https://forum.qt.io/post/311044</guid><dc:creator><![CDATA[raven-worx]]></dc:creator><pubDate>Tue, 02 Feb 2016 15:53:33 GMT</pubDate></item></channel></rss>