Python Create a real circle button in pyqt5

本小妞迷上赌 提交于 2019-12-01 12:23:25

One possible solution is to use setMask(), the advantage of this implementation is that the click event will only be for points inside the region.:

class HoverButton(QtWidgets.QToolButton):
    def __init__(self, parent=None):
        super(HoverButton, self).__init__(parent)
        self.setStyleSheet('''border-image: url("imagen.jpg")''')

    def resizeEvent(self, event):
        self.setMask(QtGui.QRegion(self.rect(), QtGui.QRegion.Ellipse))
        QtWidgets.QToolButton.resizeEvent(self, event)

Before:

After:

In your case the QRect() that is passed to QRegion must be adapted to the size of your image since the circular element is smaller than your image, a much easier way is to edit your image making the circle perfectly inscribed in your image as my image is.

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