How to draw a QPixmap at pos(0, 0) including margi

2019-08-02 16:51发布

问题:

Inside SidebarButton.paintEvent(), I want to draw a pixmap at position x=0, y=0. When margin:0px is set, this works fine. But if I increase for example margin-top to 30px, drawPixmap(0, 0, ...) paints inside the margin. I've expected that if I set a top margin, drawPixmap(0, 0, ...) starts at (0, 30).

So my question is: how can I draw at the correct position? A simple solution would be drawPixmap(0, getTopMargin() + getTopPadding(), ...) But I can't find any function which gives me the css value for margin-top and others.

BaseButton is derived from QPushButton:

class SidebarButton(BaseButton):
    def __init__(self, parent):
        super().__init__(parent)

    def paintEvent(self, e):
        super().paintEvent(e)
        p = QPainter(self)
        if self._bgimage is not None:
            pix = self.prepareIcon()
            # p.setRenderHint(QPainter.HighQualityAntialiasing);
            p.drawPixmap(0, 0,
                         pix.scaled(QSize(self.width(), self.width()) / 1.8,
                                    Qt.IgnoreAspectRatio,
                                    Qt.SmoothTransformation))

Edit:

Accessing padding from stylesheet in QT this recommends to use contentsRect() to get the content rectangle. But contentsRect().top() always returns 0 but I think it should return 30. The box model is supported by QPushButton, btw.

标签: qt pyqt qt5 pyqt5