How to draw a QPixmap at pos(0, 0) including margin/padding sizes

Deadly 提交于 2020-01-05 06:50:50

问题


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.

来源:https://stackoverflow.com/questions/42636084/how-to-draw-a-qpixmap-at-pos0-0-including-margin-padding-sizes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!