Results 1 to 8 of 8

Thread: Problems with drawing image in paintEvent of QTreeView

  1. #1
    Tito Serenti Guest

    Default Problems with drawing image in paintEvent of QTreeView

    Hi, I'm trying to put a background image in a QTreeView, but I can't use styles because the image needs to be loaded from a QByteArray. I've implemented this paint event, and the image gets drawn, but when I scroll the tree view, it doesn't re-draw the image. It just scrolls off the view and disappears. When I click inside the tree view or re-size, it gets re-drawn where it's supposed to be. Any ideas on how I can make the tree view redraw the image while scrolling? Thank you in advance!

    Qt Code:
    1. void TreeView::paintEvent(QPaintEvent *event)
    2. {
    3. QPainter painter(viewport());
    4. painter.drawImage(imageLeftMargin, imageTopMargin, image);
    5. painter.end();
    6. QTreeView::paintEvent(event);
    7. event->accept();
    8. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 24th December 2008 at 11:20. Reason: missing [code] tags

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with drawing image in paintEvent of QTreeView

    try calling update of tree view on scroll of the view

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problems with drawing image in paintEvent of QTreeView

    I suppose QTreeView::drawRow() or a custom delegate would be better place to draw such things.
    J-P Nurmi

  4. #4
    Tito Serenti Guest

    Default Re: Problems with drawing image in paintEvent of QTreeView

    Thank you for your suggestions. Unfortunately neither calling update (nor repaint) from any scrolling events helps. It's really odd, because it should, theoretically. Drawing the picture inside drawRow doesn't help either. It still doesn't update it when scrolling. I'll look into creating a custom delegate... I was hoping it'd be something simple, heh. I'm translating an application from Delphi, where those kinds of things are pretty straightforward. But then, I can't complain, because I like Qt infinitely better on most other fronts.

    The background picture works really nicely with styles and that would be my preferred solution, but I have to save the picture into a temporary file to load it via a style (background-image: url(xxx)). Is there a way to use this method without saving the file to disk? I tried creating a custom resource, but it only works with rcc files, and I don't want to have to run an external resource compiler to create an rcc file from the picture, since I'll need to save the picture to disk to do that anyway. Any further suggestions before I spend the next week trying to figure out how to create a custom delegate? (Which might or might not work?) Thank you for your time!

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problems with drawing image in paintEvent of QTreeView

    Wait a minute, I think I misunderstood the problem. You want to paint a single background image for the tree view, right? Then forget about drawRow() and custom delegate. How do you calculate imageLeftMargin and imageTopMargin?
    Last edited by jpn; 24th December 2008 at 14:29. Reason: updated contents
    J-P Nurmi

  6. #6
    Tito Serenti Guest

    Default Re: Problems with drawing image in paintEvent of QTreeView

    Yes, I want to draw a single background image for the entire tree. The image itself, the left and the right margins are set by the user in the tree properties dialog. So all I need to do is just draw the image at the given coordinates. And I want it to stay there in a fixed position, like it would when applying "background-attachment: fixed". I'm glad to hear you misunderstood the question, that gives me some hope that there's a simple solution .

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problems with drawing image in paintEvent of QTreeView

    The problem is that item views (or delegates) fill the background with QPalette::Base. You can set the view background as transparent with good old palette:
    Qt Code:
    1. QPalette pal = viewport()->palette();
    2. pal.setColor(QPalette::Base, Qt::transparent);
    3. viewport()->setPalette(pal);
    To copy to clipboard, switch view to plain text mode 
    or with style sheets:
    Qt Code:
    1. viewport()->setStyleSheet("background: transparent");
    To copy to clipboard, switch view to plain text mode 
    Once you've done that, you can paint the image like you want before calling the base class implementation of paintEvent(). If you want to re-enable the base background you can do it for example like this:
    Qt Code:
    1. void TreeView::paintEvent(QPaintEvent *event)
    2. {
    3. QPainter painter(viewport());
    4. painter.fillRect(event->rect(), palette().base()); // <---
    5. painter.drawImage(imageLeftMargin, imageTopMargin, image);
    6. painter.end();
    7. QTreeView::paintEvent(event);
    8. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  8. The following user says thank you to jpn for this useful post:


  9. #8
    Tito Serenti Guest

    Default Re: Problems with drawing image in paintEvent of QTreeView

    Wonderful, it's working just like I wanted it to! Thank you for your help!

Similar Threads

  1. Problems with QString
    By cyberboy in forum Qt Programming
    Replies: 2
    Last Post: 13th October 2008, 08:18
  2. background image in QTreeView
    By momesana in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2007, 06:25

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.