Results 1 to 7 of 7

Thread: Adding image in background

  1. #1
    Join Date
    Jun 2009
    Posts
    29
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Adding image in background

    How Can I add image in the background of my application?

    Say, I am drawing a QGraphicsItem in a QGraphicsScene and I want to add some background from an image( may be svg or png). And the background can be plain or tiled using the image.

    Thank You

  2. #2
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Adding image in background

    in your graphicsView class use
    Qt Code:
    1. setBackgroundBrush(QPixmap(":/images/myimage.jpg"));
    To copy to clipboard, switch view to plain text mode 

    also
    setCacheMode(CacheBackground); //makes better view
    "Behind every great fortune lies a crime" - Balzac

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Adding image in background

    Do you mean the background of your whole application or of your QGraphicsView?

  4. #4
    Join Date
    Jun 2009
    Posts
    29
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Adding image in background

    I meant background of the whole application.

  5. #5
    Join Date
    Jun 2009
    Posts
    29
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Adding image in background

    I implemented the drawBackGround() virtual function of QGraphicsScene:

    void GraphicsScene::drawBackground(QPainter* painter, const QRectF &rect){

    QSvgRenderer* m_renderer = new QSvgRenderer(m_gameTheme->current());
    QPixmap pix;

    pix = QPixmap(sceneRect().size().toSize());
    pix.fill(Qt::transparent);
    m_renderer->render(painter,"BackGround");

    painter->drawPixmap(0,0,pix);

    }


    But, the in the output I saw that the whole Screen is not covered. I found, (0,0) is not right from the corner. What Should be the problem?

    Mahfuz

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Adding image in background

    If you want a background for the whole application you need to set it on your QMainWindow. If it should only be for the view use QGraphicsView, because QGraphicsScene is only a part of the view!

    --> QGraphicsView::drawBackground().

  7. #7
    Join Date
    Jun 2009
    Posts
    29
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Adding image in background

    I have implemented the same funcation for QGraphicsView, but got the same output in both case.

Similar Threads

  1. Replies: 3
    Last Post: 18th May 2012, 10:12
  2. Replies: 10
    Last Post: 10th November 2010, 03:12
  3. Replies: 3
    Last Post: 9th September 2009, 04:34
  4. QCalendarWidget background image
    By khcbabu in forum Newbie
    Replies: 0
    Last Post: 31st March 2009, 12:51
  5. How to stretch background image used in brush-palette
    By Hookem in forum Qt Programming
    Replies: 5
    Last Post: 24th December 2008, 17:03

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.