Results 1 to 5 of 5

Thread: Set background image in QMainWindow

  1. #1
    Join Date
    Apr 2009
    Posts
    10
    Thanks
    5

    Default Set background image in QMainWindow

    hi,

    I have a problem when i try to load a background image in a QMainWindow.
    i override the paintevent method with the next code:
    Qt Code:
    1. void MainWindow::paintEvent(QPaintEvent *pe)
    2. {
    3.  
    4. QPainter* pPainter = new QPainter(this);
    5. pPainter->drawPixmap(rect(), QPixmap(":/images/metal.png"));
    6. delete pPainter;
    7. QWidget::paintEvent(pe);
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 

    I think the code is ok, but i am not sure!!! It doesnt load any bg image.
    the image size has to match with mainwindow size?
    any ideas??
    thank you.

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Set background image in QMainWindow

    I think the following line of code is drawing on top of your image and that is why you are not able to see the image

    Qt Code:
    1. QWidget::paintEvent(pe);
    To copy to clipboard, switch view to plain text mode 

    Remove the following line, make sure that the image file is part of your resources and then try.

  3. #3
    Join Date
    May 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Set background image in QMainWindow

    pPainter->drawPixmap(rect(), QPixmap(":/images/metal.png"));
    Maybe you should change this line to
    pPainter->drawPixmap(rect(), QPixmap("./images/metal.png"));

  4. #4
    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: Set background image in QMainWindow

    Instead of overriding the main window, you can set the background through palette
    QPalette::setBrush with QPalette::Window.
    And I hope you you know how to make brush of pixmap

  5. #5
    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: Set background image in QMainWindow

    using palette for setting background
    Qt Code:
    1. QPixmap pixmap("jughead-jones_large.png");
    2. QPalette p = w.palette();
    3. p.setBrush(QPalette::Base, pixmap);
    4. w.setPalette(p);
    5. w.resize(pixmap.size())
    To copy to clipboard, switch view to plain text mode 
    "Behind every great fortune lies a crime" - Balzac

  6. The following user says thank you to wagmare for this useful post:

    Cupidvogel (3rd January 2016)

Similar Threads

  1. Replies: 10
    Last Post: 10th November 2010, 03:12
  2. QCalendarWidget background image
    By khcbabu in forum Newbie
    Replies: 0
    Last Post: 31st March 2009, 12:51
  3. How to stretch background image used in brush-palette
    By Hookem in forum Qt Programming
    Replies: 5
    Last Post: 24th December 2008, 17:03
  4. Setting background image of QFrame
    By Claymore in forum Qt Programming
    Replies: 2
    Last Post: 12th February 2007, 19:50
  5. background image in QTreeView
    By momesana in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2007, 06:25

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.