Results 1 to 9 of 9

Thread: Repainting a QGraphicsView

  1. #1
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Repainting a QGraphicsView

    Hi! Is it possible to redraw a QGraphicsView so that, in case it has the focus, it has a border around? I tried to paint it like any other widget, overriding the paintEvent method, but it seems nothing happens. Is there any other way?
    Thanks for any advice!

  2. #2
    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: Repainting a QGraphicsView

    Could you show the relevant code, please? Did you remember to open the painter on the viewport as instructed in QAbstractScrollArea:aintEvent() docs?
    J-P Nurmi

  3. #3
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Repainting a QGraphicsView

    mmh... in the doc I found that the paintEvent method is reimplemented from QWidget:aintEvent(...). So, I did what I usually do with QWidgets.
    Qt Code:
    1. QPainter painter;
    2. painter.setBrush(QBrush(Qt::gray));
    3. painter.drawRect(QRect(0, 0, this->width(), this->height()));
    To copy to clipboard, switch view to plain text mode 
    Then, I painted the scene, ad I saw this cannot be omitted:
    Qt Code:
    1. QGraphicsView::paintEvent(event);
    To copy to clipboard, switch view to plain text mode 
    What do you mean by "open the painter"? It seems I'm lacking something...
    Thanks for your answer!

  4. #4
    Join Date
    Nov 2009
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Repainting a QGraphicsView

    Hi Can you please let me know where you are trying to draw the border? On Scene or View?
    Gopikrishna

  5. #5
    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: Repainting a QGraphicsView

    Show us the paintEvent of your view class.
    Probably you are first painting the border and then calling the base class paintEvent. This will override your painting !
    You will need to first call the base class function and then draw after it.

  6. #6
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Repainting a QGraphicsView

    This is what I wrote:
    Qt Code:
    1. void MyView::paintEvent(QPaintEvent* event) {
    2. QGraphicsView::paintEvent(event);
    3. QPainter painter(this);
    4. painter.setBrush(QBrush(Qt::green));
    5. painter.drawRect(QRect(0, 0, this->width(), this->height()));
    6. }
    To copy to clipboard, switch view to plain text mode 
    I can't understand. Maybe some other property I don't remember inserting is overwriting the background...

    EDIT: I noticed in debug mode I get this:
    warning: QPainter::begin: Paint device returned engine == 0, type: 1
    warning: QPainter::setBrush: Painter not active
    warning: QPainter::drawRects: Painter not active
    Last edited by Luc4; 28th April 2010 at 09:16.

  7. #7
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Platforms
    Unix/X11 Windows

    Default Re: Repainting a QGraphicsView

    try this :

    Qt Code:
    1. void MyView::paintEvent(QPaintEvent * e)
    2. {
    3. // 1. draw scene items
    4. QGraphicsView::paintEvent(e);
    5.  
    6. // 2. draw zoom area if any
    7. if( m_zoomArea.width()!=0 || m_zoomArea.height()!=0 )
    8. {
    9. QPainter painter( viewport() );
    10. painter.save() ;
    11. // paint here
    12. painter.restore() ;
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    i.e., draw on view's viewport instead

  8. #8
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Repainting a QGraphicsView

    It seems it is not redrawing correctly the scene, lines remain in the area. This is what I wrote:
    Qt Code:
    1. void MyView::paintEvent(QPaintEvent* event) {
    2. QGraphicsView::paintEvent(event);
    3. QPainter painter(this->viewport());
    4. painter.save();
    5. QPen pen;
    6. pen.setColor(SELECTION_BORDER_AREA_QCOLOR);
    7. pen.setWidth(GoodiesStore::SELECTION_BORDER_AREA_WIDTH);
    8. painter.setPen(pen);
    9. painter.drawRect(QRect(0, 0, this->width(), this->height()));
    10. painter.restore();
    11. }
    To copy to clipboard, switch view to plain text mode 
    When I scroll the view, it seems lines are scrolled too.
    Thank you for your help!

  9. #9
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Repainting a QGraphicsView

    In case anyone else need this, I didn't know how to solve this so I placed a label under the QGraphicsView and made it larger than the QGraphicsView. The result is exactly the same.

Similar Threads

  1. Very slow repainting ofPixmaps in QGraphicsView images
    By drexiya in forum Qt Programming
    Replies: 3
    Last Post: 21st October 2009, 18:13
  2. QGraphicsView, OpenGL and repainting/updating
    By Amargedon in forum Qt Programming
    Replies: 2
    Last Post: 19th January 2009, 12:03
  3. QGraphicsItem not repainting
    By eijnuhs in forum Qt Programming
    Replies: 3
    Last Post: 20th September 2008, 08:54
  4. repainting through paintEvent() function
    By salmanmanekia in forum Qt Programming
    Replies: 5
    Last Post: 6th August 2008, 12:50
  5. Repainting widget
    By fear in forum Qt Programming
    Replies: 3
    Last Post: 26th March 2008, 08:37

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.