class FieldLabel(QtGui.QLabel):
positionData = {"1":{"x":13,"y":20},"2":{"x":28,"y":19},"3":{"x":17,"y":21}}
def __init__(self, image_path):
QtGui.QLabel.__init__(self)
self.field = QtGui.QPixmap("field.png")
self.setPixmap(self.field.scaled(self.size(),
QtCore.Qt.KeepAspectRatio))
self.setSizePolicy(QtGui.QSizePolicy.Expanding,
QtGui.QSizePolicy.Expanding)
self.setAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter)
def paintEvent(self,e):
draw = QtGui.QPainter()
draw.begin(self)
draw.setBrush(QtCore.Qt.NoBrush)
draw.setPen(QtCore.Qt.blue)
draw.drawRect(0,0,10,10)
draw.end()
答案 0 :(得分:1)
paintEvent
负责绘制小部件中的所有内容。由于您要覆盖它,因此不会调用QLabel
绘制QPixmap
的默认实现。
所以,首先你应该让QLabel
做它的绘画,然后你可以随意画它:
def paintEvent(self,e):
# call the base implementation to paint normal interface
super(FieldLabel, self).paintEvent(e)
# then paint over it
draw = QtGui.QPainter()
draw.begin(self)
draw.setBrush(QtCore.Qt.NoBrush)
draw.setPen(QtCore.Qt.blue)
draw.drawRect(0,0,10,10)
draw.end()