View Full Version : QPixmap as background of QWidget

15th March 2013, 18:33
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.

originalPixmap = QPixmap::grabWindow(desk.winId());
ui->mainWidget->setStyleSheet("background-image: ???"); //what here?

Any ideas?

//Ok, it was easier than I thought. This is the answer:

originalPixmap = QPixmap::grabWindow(desk.winId());
QPalette palette;
palette.setBrush((this)->backgroundRole(), QBrush(originalPixmap));

15th March 2013, 19:52
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.

17th March 2013, 16:03
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.