Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Just to complete the whole picture: further one can find the code to manage the QWidget as QListView item using delegates.</p> <p>I finally found out how to make it work within the subclass of QStyledItemDelegate using its paint(...) method. </p> <p>It seems more effective for performance than previous solution, but this statement one needs to verify =) by investigation what does setIndexWidget() do with created QWidget.</p> <p>So finally, here is the code:</p> <pre><code>class PackageListItemWidget: public QWidget </code></pre> <p>.....</p> <pre><code>class PackageListItemDelegate: public QStyledItemDelegate </code></pre> <p>.....</p> <pre><code>void PackageListItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &amp;option, const QModelIndex &amp;index ) const { // here we have active painter provided by caller // by the way - we can't use painter-&gt;save() and painter-&gt;restore() // methods cause we have to call painter-&gt;end() method before painting // the QWidget, and painter-&gt;end() method deletes // the saved parameters of painter // we have to save paint device of the provided painter to restore the painter // after drawing QWidget QPaintDevice* original_pdev_ptr = painter-&gt;device(); // example of simple drawing (selection) before widget if (option.state &amp; QStyle::State_Selected) painter-&gt;fillRect(option.rect, option.palette.highlight()); // creating local QWidget (that's why i think it should be fasted, cause we // don't touch the heap and don't deal with a QWidget except painting) PackageListItemWidget item_widget; // Setting some parameters for widget for example // spec. params item_widget.SetPackageName(index.data(Qt::DisplayRole).toString()); // geometry item_widget.setGeometry(option.rect); // here we have to finish the painting of provided painter, cause // 1) QWidget::render(QPainter *,...) doesn't work with provided external painter // and we have to use QWidget::render(QPaintDevice *,...) // which creates its own painter // 2) two painters can't work with the same QPaintDevice at the same time painter-&gt;end(); // rendering of QWidget itself item_widget.render(painter-&gt;device(), QPoint(option.rect.x(), option.rect.y()), QRegion(0, 0, option.rect.width(), option.rect.height()), QWidget::RenderFlag::DrawChildren); // starting (in fact just continuing) painting with external painter, provided // by caller painter-&gt;begin(original_pdev_ptr); // example of simple painting after widget painter-&gt;drawEllipse(0,0, 10,10); }; </code></pre>
 

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