Search code examples
pythonstylesheetpyside

pyside set background image via style sheet not working


I'm Trying to set the background image (which is located in the same folder as the application) of a QGridLayout widget via pyside to no avail I have looked at all the tuts, docs and forum posts regarding stylesheets' but still i cant figure out what I'm doing wrong.

from PySide.QtCore import *
from PySide.QtGui import *
import sys

class Main(QWidget):

    def __init__(self, parent=None):
        super(Main, self).__init__(parent)

        j = 0
        pos =  [(0, 0), (0, 1), (0, 2), (0, 3),
                (1, 0), (1, 1), (1, 2), (1, 3),
                (2, 0), (2, 1), (2, 2), (2, 3),
                (3, 0), (3, 1), (3, 2), (3, 3),
                (4, 0), (4, 1), (4, 2), (4, 3),
                (5, 0), (5, 1), (5, 2), (5, 3)]

        grid  = QGridLayout(self)
        items = ["one", "Two", 'Three','four','five']

        for e in items:
            picture = ImageLabel("Book.png", self)
            picture.name = e
            picture.setFixedSize(128,128)
            picture.mousepos = str(picture.pos())
            picture.imageClicked.connect(self.anotherSlot)
            grid.addWidget(picture, pos[j][0], pos[j][1])
            j = j + 1
            picture.setToolTip('This is' + str(e) + ' widget')
            picture.imageHovered.connect(self.Item_Hovered)

        self.setStyleSheet("QGridLayout {background-image: url(./image.jpg) }");


    def anotherSlot(self):
        sender = self.sender()
        print "I clicked item " + str(sender.name)

    def Item_Hovered(self):
        sender = self.sender()
        print "I'm Hovering over " + str(sender.name)

class ImageLabel(QLabel):

    imageClicked = Signal(str) # can be other types (list, dict, object...)
    imageHovered = Signal(str)
    imageLeave   = Signal(str)

    def __init__(self, image, parent=None):
        super(ImageLabel, self).__init__(parent)        
        self.setPixmap(image)
        # self.setMouseTracking(True)

    def mousePressEvent(self, event):
        print "from ImageLabel.mousePressEvent()"
        self.imageClicked.emit("Image Clicked")

    def enterEvent(self, event):
        print "from ImageLabel.enterEvent()"
        self.imageHovered.emit("Hovering")

    def leaveEvent(self, event):
        print "from ImageLabel.leaveEvent()"
        self.imageLeave.emit("Hovering No More")


a = QApplication([])
m = Main()
m.show()
sys.exit(a.exec_())

any advice would be appreciated.


Solution

  • even if it works you'll not get the expected result, the only way I achieve correct background paint is overriding paintEvent

    tile = QtGui.QPixmap("x.png")
    
    def paintEvent(self, pe):
        painter = QtGui.QPainter(self)
        painter.drawTiledPixmap(self.rect(), tile)
        super(Console, self).paintEvent(pe)
    

    Console is my class class Console(QtGui.QMainWindow): The example applies to the whole window.

    I'm not sure the painter constructor can be moved away from paintEvent

    Update: looks like QPainter needs to be created on each paintEvent

    I've found an easiest way without override the paintEvent

    palette = QtGui.QPalette()
    palette.setBrush(QtGui.QPalette.Background, tile)
    self.setPalette(palette)