PyQt5:屏蔽的QPushButton的抗锯齿失败

时间:2018-10-10 18:47:00

标签: python pyqt pyqt5

请考虑以下最小示例:

import sys

from PyQt5 import QtCore, QtGui, QtWidgets


class FooPushButton(QtWidgets.QPushButton):
    def __init__(self):
        super().__init__()

        self.setFixedWidth(200)
        self.setFixedHeight(200)

        self.setMask(QtGui.QRegion(self.rect(), QtGui.QRegion.Ellipse))

    def paintEvent(self, event: QtGui.QPaintEvent):
        painter = QtGui.QPainter(self)
        painter.setRenderHint(QtGui.QPainter.Antialiasing)

        pen = QtCore.Qt.NoPen
        brush = QtGui.QBrush(QtCore.Qt.red)

        painter.setPen(pen)
        painter.setBrush(brush)

        painter.drawEllipse(self.rect())

        painter.end()


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()

        graphicsScene = QtWidgets.QGraphicsScene()
        graphicsScene.addWidget(FooPushButton())

        graphicsView = QtWidgets.QGraphicsView()
        graphicsView.setScene(graphicsScene)

        self.setCentralWidget(graphicsView)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)

    mainWindow = MainWindow()
    mainWindow.show()

    sys.exit(app.exec())

这会将QPushButton舍入为QGraphicsScene,但是, 抗锯齿渲染提示未得到尊重。删除对setMask的呼叫会使抗锯齿再次起作用。我想这是因为抗锯齿实际上并未应用到由屏蔽操作导致的可见按钮边框上。

我该如何解决?

0 个答案:

没有答案