pyqt5 给特定QWidget QLabel设置圆角

722 阅读2分钟

要给QWidget设置圆角,可以使用QPainterPath和QPainter来实现。下面是一个示例代码,其中ButtonWidget是要设置圆角的QWidget类:

from PyQt5.QtWidgets import QWidget, QPushButton, QApplication
from PyQt5.QtGui import QPainter, QPainterPath
from PyQt5.QtCore import Qt

# 自定义QPushButton,实现圆角按钮
class RoundButton(QPushButton):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setStyleSheet('''
            border-radius: 10px; 
            background-color: blue; 
            color: white;
            padding: 8px;
            ''')

# 自定义QWidget,绘制圆角边框
class ButtonWidget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.button = RoundButton(self)

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing)
        painter.setPen(Qt.NoPen)
        painter.setBrush(Qt.white)
        
        path = QPainterPath()
        w, h = self.width(), self.height()
        radius = 20  # 圆角半径
        path.addRoundedRect(0, 0, w, h, radius, radius)
        painter.drawPath(path)
        
if __name__ == '__main__':
    app = QApplication([])
    widget = ButtonWidget()
    widget.setWindowTitle('圆角 QWidget')
    widget.resize(200, 100)
    widget.show()
    app.exec_()

在这个示例中,我们创建了一个RoundButton类,使用border-radius来设置QPushButton的圆角。然后创建了一个ButtonWidget类,在paintEvent中使用QPainterPath和QPainter绘制圆角边框。最后,在主函数中创建了这个QWidget并显示出来。

注意:如果要给QWidget设置圆角,需要在paintEvent中进行,因为在显示QWidget时,其背景色会覆盖之前的绘制。

pyqt5中QLabel并没有直接提供圆角的属性,但可以通过以下步骤来实现:

  1. 创建新的QLabel子类并重写paintEvent方法。

  2. 在paintEvent方法中绘制带有圆角的背景。

  3. 调用父类的paintEvent方法绘制文本。

以下是完整的代码示例:

from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPainter, QColor, QBrush, QPen
from PyQt5.QtWidgets import QApplication, QWidget, QLabel


class RoundedLabel(QLabel):
    def __init__(self, text="", parent=None):
        super().__init__(text, parent)
        self.setStyleSheet("border-radius: 10px;")

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing)
        painter.setPen(Qt.NoPen)

        radius = 10
        rect = self.rect()
        path = QPainterPath()
        path.moveTo(rect.x() + radius, rect.y())
        path.lineTo(rect.width() - radius, rect.y())
        path.quadTo(rect.width(), rect.y(), rect.width(), rect.y() + radius)
        path.lineTo(rect.width(), rect.height() - radius)
        path.quadTo(rect.width(), rect.height(), rect.width() - radius, rect.height())
        path.lineTo(rect.x() + radius, rect.height())
        path.quadTo(rect.x(), rect.height(), rect.x(), rect.height() - radius)
        path.lineTo(rect.x(), rect.y() + radius)
        path.quadTo(rect.x(), rect.y(), rect.x() + radius, rect.y())
        painter.setBrush(QBrush(QColor("#f7f7f7")))
        painter.drawPath(path)

        super().paintEvent(event)


if __name__ == "__main__":
    app = QApplication([])
    window = QWidget()
    label = RoundedLabel("Rounded Label")
    label.setAlignment(Qt.AlignCenter)
    window.setLayout(QVBoxLayout())
    window.layout().addWidget(label)
    window.show()
    app.exec_()

在上面的代码中,我们首先创建了一个新的QLabel子类RoundedLabel并重写了它的paintEvent方法。在paintEvent方法中,我们使用QPainterPath绘制了带有圆角的矩形背景。

最后,我们也可以通过设置border-radius属性使得RoundedLabel带有圆角属性。