[Interest] Drawing a background image in QTreeWidget [SOLVED]
Bob Hood
bhood2 at comcast.net
Mon Aug 6 23:40:30 CEST 2018
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 at qt-project.org http://lists.qt-project.org/mailman/listinfo/interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20180806/5ac1aaa2/attachment.html>
More information about the Interest
mailing list