gpt4 book ai didi

python - pyside 通过样式表设置背景图像不起作用

转载 作者:行者123 更新时间:2023-12-01 05:55:21 27 4
gpt4 key购买 nike

我正在尝试通过 pyside 设置 QGridLayout 小部件的背景图像(位于与应用程序相同的文件夹中),但无济于事我已经查看了有关样式表的所有图、文档和论坛帖子,但仍然我不知道我做错了什么。

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_())

如有任何建议,我们将不胜感激。

最佳答案

即使它有效,你也不会得到预期的结果,我实现正确背景绘制的唯一方法是重写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 是我的类 class Console(QtGui.QMainWindow):该示例适用于整个窗口。

我不确定 Painter 构造函数是否可以从 paintEvent 中移走

更新:看起来需要在每个paintEvent上创建QPainter

我找到了一种最简单的方法,无需覆盖 paintEvent

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

关于python - pyside 通过样式表设置背景图像不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12829911/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com