Solved it.  Drawing in the viewport's rectangle instead of the event's keeps the background image properly positioned and updated when selections change:

   void SeatTree::paintEvent(QPaintEvent *event)
   {
        // draw in the viewport rectangle coordinates instead of the
        // event's to keep the background image properly updated

   *QRect r = viewport()->rect();*

        QPainter painter(viewport());
   painter.fillRect(r, palette().base());

        int left = r.width() - server_image.width();
        int top = r.height() - server_image.height();
   painter.drawPixmap(left, top, server_image);

        painter.end();

   QTreeWidget::paintEvent(event);

   event->accept();
   }


On 8/3/2018 12:31 PM, Bob Hood wrote:
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.


_______________________________________________ Interest mailing list Interest@qt-project.org http://lists.qt-project.org/mailman/listinfo/interest

_______________________________________________
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest

Reply via email to