我正在尝试通过 pyside 设置 QGridLayout 小部件的背景图像(与应用程序位于同一文件夹中)但无济于事我查看了有关样式表的所有 tuts、文档和论坛帖子,但我仍然无法弄清楚找出我做错了什么。
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_())
任何意见,将不胜感激。