PDA

View Full Version : How to stretch background image used in brush-palette



Hookem
16th December 2008, 23:00
I am trying to make a header label with text over a background image. I'm using the image like a spacer image, so it should stretch the width of the label. I have been able to get the image in the background; however I can't figure out how to make it stretch. Here is the code:



palette = QtGui.QPalette()

brush = QtGui.QBrush(QtGui.QColor(170, 255, 0))
brush.setTexture(QtGui.QPixmap("Images/small_border.png"))
brush.setStyle(QtCore.Qt.TexturePattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Window, brush)

self.ui.label_2.setPalette(palette)

The image is being displayed in the TexturePattern, just as it should given the style. However I don't see a way to set the style so that it will stretch the image to fill the space. How can I do that?

Thanks

aamer4yu
17th December 2008, 05:29
May be you can try QLabel::setScaledContents

Hookem
17th December 2008, 17:09
I think that only applies to objects in the foreground. It works like I want it to if I load a picture into the label. The problem is that I can't get any text on top of it at that point because you have to pick one or the other.

aamer4yu
17th December 2008, 17:58
In that case, scale the pixmap to the label's size.

QPixmap pix = QtGui.QPixmap("Images/small_border.png").scaled(label_2->size());
brush.setTexture(pix);

Somethign like that.. hope u get the idea

Hookem
23rd December 2008, 00:43
I didn't know you could scale it like that, so I tried to scale it against the main centralWidget to give it a full backdrop. It's a case where I believe it's working properly but not as desired.

When I scale it against the main frame it sizes it up properly to give a nice textured pattern of the image. However I can't find any way to make it not texture and just give me a solid image that is scaled to be the size of the widget.

If I don't specify the texture than it will display the image in it's natural size. So if I wanted to use a background picture I can just make it larger than most monitors can display it, and I have my background image.

However I've tried all kinds of ways but still can't find a way to make the image simply scale to the size of target widget. I've tried creating the brush with a pix image as the argument, but I guess it defaults to be a texture. Is putting an image into a Texture the only way to insert it into a brush, or is there any other way?

Hookem
24th December 2008, 18:03
<bump> Is this functionality just not available in pyqt4?