[Interest] Drawing a background image in QTreeWidget

Bob Hood bhood2 at comcast.net
Fri Aug 3 20:31:20 CEST 2018


I am (whimsically) trying to draw a background image in a QTreeWidget.  I have 
this simple code working in a QTreeWidget subclass:

    SeatTree::SeatTree(const QString& prefix, QWidget *parent)
         : QTreeWidget(parent)
    {
         server_image = QPixmap(QString(":/images/%1_Server.png").arg(prefix));

         image_dimensions.setX(server_image.width());
         image_dimensions.setY(server_image.height());

         QPalette pal = viewport()->palette();
         pal.setColor(QPalette::Base, Qt::transparent);
         viewport()->setPalette(pal);
    }

    void SeatTree::paintEvent(QPaintEvent *event)
    {
         QPainter painter(viewport());
         painter.fillRect(event->rect(), palette().base());

         // added this predicate to prevent the image from being drawn
         // in the confines of row
         if(event->rect().width() >= image_dimensions.x() &&
            event->rect().height() >= image_dimensions.y())
         {
             int left = event->rect().width() - server_image.width();
             int top = event->rect().height() - server_image.height();
             painter.drawPixmap(left, top, server_image);
         }

         painter.end();

         QTreeWidget::paintEvent(event);

         event->accept();
    }

And this draws the image exactly as I expect it to.  However, the rows of the 
Tree damage the background image in various scenarios, and it does not get 
corrected unless the entire Tree gets re-drawn.

Do I need to move this action into another painting function, e.g. the 
drawTree() method of a QTreeView, to get it to show without being damaged?  Or 
can I do this with some additional acrobatics in the QTreeWidget paintEvent()?

Thanks for any insights.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20180803/0fee73a9/attachment.html>


More information about the Interest mailing list