Results 1 to 10 of 10

Thread: QGraphicsScene - drawBackground, only one picture

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

    Default QGraphicsScene - drawBackground, only one picture

    Hi,

    I'm coding a programm with a QGraphicsScene and a QGraphicsView of course.
    In the GraphicsView I want to use a picture as the backgroundBrush of this View.

    I have coded a widget to choose a picture with a QFileDialog. Now you can choose a picture and it will shows on the background of the view...it works fine!

    But I have a problem:

    The picture fills the complete background. If the background/the View greater than the picture (I have implemented a Zoom too), the picture will repeated on the complete background. But I want that the picture will show only one time at the background, the other space of the background shall be colored in white.

    Here is my code:

    Qt Code:
    1. QImage *loadedPicture = new QImage( sPathOfPicture );
    2.  
    3. //Brush erstellen und Image als Brush setzen
    4. brushBackground = new QBrush( *loadedPicture );
    5.  
    6. //Hintergrund-Brush der Zeichenfläche zuweisen
    7. this->setBackgroundBrush( *brushBackground );
    8. this->setCacheMode(QGraphicsView::CacheBackground);
    To copy to clipboard, switch view to plain text mode 

    I have googled about this problem, but I can't find any solutions except like "You can reimplement the drawBackground-Methode", etc. but because I'm a newbie, I don't know how I have to do that.

    Please, can anyone help me with my problem!

    Thanks

    PS.: I hope you can read my bad english!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsScene - drawBackground, only one picture

    Yeah, you have to reimplement drawBackground()

    Subclass the view class and write new content for drawBackground() where you will use the given painter to paint the image once, without repeating.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jul 2012
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsScene - drawBackground, only one picture

    Hi,

    thanks for your answer.

    So, you don't tell me new things

    My problem: I'm still a newbie with QT and I have very big problems to reimplement the drawBackground-Methode because I don't know the parameters of this method. For example: Which QPainter is needed to reimplement that method. I think I cannot use any Instance of a QPainter, I can?

    Is there a special QPainter für the QGraphicsView?

    My programm is almost finished, this is my last problem and I have no idea how I can solve this. Can you give me more explanation to reimplement this methode or can tell me an easier way to solve my problem, please?

    Thanks!

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsScene - drawBackground, only one picture

    Quote Originally Posted by Gruwe View Post
    I'm still a newbie with QT
    With Qt or with C++?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jul 2012
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsScene - drawBackground, only one picture

    Quote Originally Posted by wysota View Post
    With Qt or with C++?
    More or less with both! ;-)

    Ok, give me a chance:

    1. I have a QGraphicsScene and a QGraphicsView that works correctly.
    2. I have a picture that I want to display in the View.
    3. The drawBackground-Methode needs a pointer to a QPainter and a QRect.

    My first idea was:

    Make a new instance of QPainter, draw in his painter or paste a QImage, etc. Then give this QPainter as an parameter to the drawBackground(). The compiler say that this is ok, but it doesn't work.

    Now, I don't know another possible solutions. What do You want to give me help?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsScene - drawBackground, only one picture

    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'm sorry but this is not a place to teach you C++. Get a good C++ book and read it. You can start with downloading Thinking in C++.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #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 13:04.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsScene - drawBackground, only one picture

    What do you mean "which painter"? You get a painter and the exposed rect and you are supposed to use the painter to draw whatever you want in the scene coordinates determined by the rectangle.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Jul 2012
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsScene - drawBackground, only one picture

    Hi,

    I have now coded this:

    Qt Code:
    1. void GraphicsView::drawBackground(QPainter *painter, const QRectF &rect)
    2. {
    3. QRectF &theRect = newRect;
    4.  
    5. painter->fillRect(theRect, this->backgroundBrush());
    6. }
    To copy to clipboard, switch view to plain text mode 

    In the method, where I can choose the picture for the background I define a QRectF with the width and the height of the picture. That QRectF is named newRect.

    With this code it works correctly I think. But is this the right and best way?


    Thanks

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsScene - drawBackground, only one picture

    I would rather use QPainter::drawImage()
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 3
    Last Post: 9th March 2012, 00:51
  2. Replies: 1
    Last Post: 5th September 2011, 21:26
  3. How to create PIP (Picture In Picture) on QwebView
    By luckychap in forum Qt Programming
    Replies: 0
    Last Post: 18th July 2011, 13:47
  4. drawBackground function.....
    By dreamer in forum Qt Programming
    Replies: 4
    Last Post: 24th April 2008, 08:28
  5. [GraphicsView] drawBackground junks around
    By durbrak in forum Qt Programming
    Replies: 5
    Last Post: 27th December 2007, 16: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.