0

guys.I have a QLabel with a pixmap-a PNG image(typically a football playground) and I wanna draw some rectangels(represent some robots) on the playground,which I use the painter class to draw actually on its container-the QLabel. But when I use the painter to draw the REC,the RECT showed but the image just turned to blank.I don't know why it failed, and could u plz do me a favor and give me some hints on that?

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()
Derek
  • 41
  • 2
  • 8

1 Answers1

1

paintEvent is responsible for drawing everything in the widget. Since you're overriding it, QLabel's default implementation, which draws the QPixmap, is not invoked.

So, first you should let the QLabel do its painting, then you can paint over it as you like:

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()
Avaris
  • 35,883
  • 7
  • 81
  • 72
  • Thank u.@Avaris That's very helpful.I didn't recognize the set of pixmap is also a paintEvent and will have some override effect. – Derek Feb 20 '13 at 14:45
  • And I'm also working on some coordinate system on that Label and pixmap.But I was suprised to find that when I got and print out some height and width integer values(using predefined methods like `.height()`and`.width()`) from the Label and the Pixmap, the values of the Pixmap is even larger than the Label's.So I guess the two classes use different measure unit mathematically.So how can I get the same measure background so I can calculate some coordinates for my paintings? – Derek Feb 20 '13 at 14:57
  • @Derek: Not they have the same unit (pixels). But you're putting the pixmap scaled to the size of `QLabel`. If you don't do any scaling or manually setting the size of label, `QLabel` will adjust itself to the size of the pixmap. – Avaris Feb 20 '13 at 16:16
  • yeah,I found you are absolutely right.I use the methods and they all return some fixed values even when the pixmap is scaled.So is there a way to get the current size value of the pixmap? I tried with some caculations by label values minus margin and intent,but that two are also fixed with 0 and -1.(It's so painful to try to paint on the flexible monitor label.) – Derek Feb 20 '13 at 19:51
  • @Derek: Just load your pixmap and its `.size()` will be the correct dimensions. – Avaris Feb 20 '13 at 21:54
  • I would rather say to load the scaled pixmap for precision, cuz I didn't explicitly define a QPixmap object for the scaled pixmap like `scaled_Pixmap=self.field.scaled(self.width(),self.height(),QtCore.Qt.KeepAspectRatio)`.LoL, finally I can find a fix myself,thank your so much. – Derek Feb 20 '13 at 22:47