Note that there are some explanatory texts on larger screens.

plurals
  1. PORender QWidget in paint() method of QWidgetDelegate for a QListView
    primarykey
    data
    text
    <p>i'm having difficulties implementing custom widget rendering in a <code>QListView</code>. I currently have a <code>QListView</code> displaying my custom model called <code>PlayQueue</code> based on <code>QAbstractListModel</code>.</p> <p>This is working fine with simple text, but now I would like to display a custom widget for each element. So I subclassed a <code>QStyledItemDelegate</code> to implement the <code>paint</code> method like this:</p> <pre><code>void QueueableDelegate::paint(QPainter *painter, const QStyleOptionViewItem &amp;option, const QModelIndex &amp;index ) const { if (option.state &amp; QStyle::State_Selected) painter-&gt;fillRect(option.rect, option.palette.highlight()); QWidget *widget = new QPushButton("bonjour"); widget-&gt;render(painter); } </code></pre> <p>The selection background is properly rendered but no widget is displayed. I tried with simple <code>QPainter</code> commands like in Qt examples, and this is working fine:</p> <pre><code>void QueueableDelegate::paint(QPainter *painter, const QStyleOptionViewItem &amp;option, const QModelIndex &amp;index ) const { if (option.state &amp; QStyle::State_Selected) painter-&gt;fillRect(option.rect, option.palette.highlight()); if (option.state &amp; QStyle::State_Selected) painter-&gt;setPen(option.palette.highlightedText().color()); painter-&gt;setFont(QFont("Arial", 10)); painter-&gt;drawText(option.rect, Qt::AlignCenter, "Custom drawing"); } </code></pre> <p>So I tried some changes like:</p> <ul> <li>Changing <code>QStyledItemDelegate</code> to <code>QItemDelegate</code></li> <li>Adding <code>painter-&gt;save()</code> and <code>painter-&gt;restore()</code> around rendering</li> <li>Setting the widget geometry to the available size</li> </ul> <p>But i'm a bit stuck now, i searched for a while on the internet, but can't find any example doing what i want, they all talk about editing widget (which is a lot easier) or custom drawn control (predefined ones, like progress bars). But here I really need a custom widget I created, containing some layout, labels &amp; pixmaps. Thanks for your help!</p> <p>I'm using Qt 4.7.3 for GCC on Ubuntu 11.04.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload