Results 1 to 10 of 10

Thread: QGraphicsScene - drawBackground, only one picture

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #7
    Join Date
    Jul 2012
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsScene - drawBackground, only one picture

    Hi,

    sorry, I think you don't understand what I mean, it is not a problem with C++, but with Qt. Let me try to explain:

    Quote Originally Posted by wysota View Post
    No. You are not supposed to call this method. You are supposed to write one. You will be given a painter and all that stuff.
    I know, that I have to write a "new" drawBackground-Methode. So first I have to make a subclass of QGraphicsView with declare a drawBackground-Methode:

    Qt Code:
    1. class View : public QGraphicsView
    2. ...
    3. {
    4. protected:
    5. virtual void drawBackground(...);
    6. ...
    7. }
    To copy to clipboard, switch view to plain text mode 

    Ok?

    Then I have to write the method:

    Qt Code:
    1. void drawBackground(...)
    2. {
    3. ...
    4. }
    To copy to clipboard, switch view to plain text mode 

    Ok? Ok!

    So, my question was: What have I to write in this NEW method? So, I don't know how the original drawBackground-method painted a Background, I took a look on the Qt-Source, especially on the source of QGraphicsView, of course!
    Let me show the source of the original drawBackground-method of QGraphicsView:

    Qt Code:
    1. /*!
    2.   Draws the background of the scene using \a painter, before any items and
    3.   the foreground are drawn. Reimplement this function to provide a custom
    4.   background for this view.
    5.  
    6.   If all you want is to define a color, texture or gradient for the
    7.   background, you can call setBackgroundBrush() instead.
    8.  
    9.   All painting is done in \e scene coordinates. \a rect is the exposed
    10.   rectangle.
    11.  
    12.   The default implementation fills \a rect using the view's backgroundBrush.
    13.   If no such brush is defined (the default), the scene's drawBackground()
    14.   function is called instead.
    15.  
    16.   \sa drawForeground(), QGraphicsScene::drawBackground()
    17. */
    18. void QGraphicsView::drawBackground(QPainter *painter, const QRectF &rect)
    19. {
    20. if (d->scene && d->backgroundBrush.style() == Qt::NoBrush) {
    21. d->scene->drawBackground(painter, rect);
    22. return;
    23. }
    24.  
    25. painter->fillRect(rect, d->backgroundBrush);
    26. }
    To copy to clipboard, switch view to plain text mode 

    Ok, this method gets a pointer on an instance of QPainter and a QRectF. Then the methode will either call the drawBackground-method of the scene or the fillRect-Method of the painter.

    In the case the method of the scene is called, this source-code will runs:
    Qt Code:
    1. /*!
    2.   Draws the background of the scene using \a painter, before any items and
    3.   the foreground are drawn. Reimplement this function to provide a custom
    4.   background for the scene.
    5.  
    6.   All painting is done in \e scene coordinates. The \a rect
    7.   parameter is the exposed rectangle.
    8.  
    9.   If all you want is to define a color, texture, or gradient for the
    10.   background, you can call setBackgroundBrush() instead.
    11.  
    12.   \sa drawForeground(), drawItems()
    13. */
    14. void QGraphicsScene::drawBackground(QPainter *painter, const QRectF &rect)
    15. {
    16.  
    17. if (d->backgroundBrush.style() != Qt::NoBrush) {
    18. if (d->painterStateProtection)
    19. painter->save();
    20. painter->setBrushOrigin(0, 0);
    21. painter->fillRect(rect, backgroundBrush());
    22. if (d->painterStateProtection)
    23. painter->restore();
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 

    You can see: Both drawBackground-method use a QPainter. But which painter? Yes, I know that it's the painter that drawBackground of the view gets as an parameter, but...I don't know how I can explain what I mean

    Ähm, it's difficult to explain my problem:

    I dont know how QGraphicsView/Scene works to use a background. Is there a QPainter or so? It looks so, or?

    Can you understand my problem now?

    edit: Or an other idea: I have only to set an BackgroundBrush. And the drawBackground is only calling from the view/scene automatically, when the scene will be rendered?
    And know I have only to write a drawBackground similiar to the original method, but I have to try that this method will paint the backgroundBrush only one time? How can I make that?
    Last edited by Gruwe; 26th July 2012 at 12:04.

Similar Threads

  1. Replies: 3
    Last Post: 8th March 2012, 23:51
  2. Replies: 1
    Last Post: 5th September 2011, 20:26
  3. How to create PIP (Picture In Picture) on QwebView
    By luckychap in forum Qt Programming
    Replies: 0
    Last Post: 18th July 2011, 12:47
  4. drawBackground function.....
    By dreamer in forum Qt Programming
    Replies: 4
    Last Post: 24th April 2008, 07:28
  5. [GraphicsView] drawBackground junks around
    By durbrak in forum Qt Programming
    Replies: 5
    Last Post: 27th December 2007, 15:53

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
  •  
Qt is a trademark of The Qt Company.