Results 1 to 3 of 3

Thread: QPixmap as background of QWidget

  1. #1
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    19
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default QPixmap as background of QWidget

    Hi all.

    I have a little problem with my application, so I'm working at program, that can be used to take screenshots of desktop, but only area which I select, next - save image to file/share via mail etc. My current layout exclude use QLabel element and set his pixmap in my viewer, so I must use QWidget object. I read documentation and I understand, that QWidget's background can be change only with CSS using. But I have no idea what I should do that, when I have an image in QPixmap; of course I want do it without saving this screenshot on hard disk.

    Qt Code:
    1. originalPixmap = QPixmap::grabWindow(desk.winId());
    2. ui->mainWidget->setStyleSheet("background-image: ???"); //what here?
    To copy to clipboard, switch view to plain text mode 

    Any ideas?
    Thanks.

    //Ok, it was easier than I thought. This is the answer:
    Qt Code:
    1. originalPixmap = QPixmap::grabWindow(desk.winId());
    2. QPalette palette;
    3. palette.setBrush((this)->backgroundRole(), QBrush(originalPixmap));
    4. (this)->setPalette(palette);
    To copy to clipboard, switch view to plain text mode 
    Last edited by Alan_K; 15th March 2013 at 19:27.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QPixmap as background of QWidget

    Alternatively, you can implement the paintEvent() for your widget, and inside it call QPainter::drawPixmap().

    If you set the palette's background brush the way you are now doing, I think the pixmap will be tiled (or clipped) if it is a different size from the widget you use to display it. When you use QPainter, you can scale the pixmap to fit the window if that's what you want.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QPixmap as background of QWidget

    I think it should also work to use the label as a container widget. Sounds weird but a QLabel is a QFrame and that is a very common container.

    Cheers,
    _

Similar Threads

  1. QPixmap's background
    By Dopt in forum Qt Programming
    Replies: 3
    Last Post: 31st May 2012, 16:20
  2. How use svg to set QWidget background
    By sanjayshelke in forum Qt Programming
    Replies: 2
    Last Post: 29th June 2009, 12:47
  3. QWidget background
    By salmanmanekia in forum Qt Programming
    Replies: 4
    Last Post: 23rd July 2008, 11:55
  4. QPixmap::grabWidget and QWidget::render loose alpha
    By EricF in forum Qt Programming
    Replies: 5
    Last Post: 11th March 2008, 09:42
  5. background image for QWidget
    By raman_31181 in forum Qt Programming
    Replies: 2
    Last Post: 8th March 2008, 12:13

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.