본문 바로가기

프로그래밍/PyQt5 GUI

50. 둥근 직사각형 그리기 (drawRoundedRect)

drawRoundedRect Class는 모서리가 둥근 직사각형을 그릴 때 사용합니다.

drawRoundedRect() 에 x, y, width, height, x-radius, y-radius의 순서로 숫자를 입력합니다.

50-1 예제: ex49

import sys
from PyQt5.QtWidgets import (QApplication, QWidget)
from PyQt5.QtGui import (QPainter, QPen)
from PyQt5.QtCore import Qt

class MyApp(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('drawRoundedRect')
        self.setGeometry(300, 300, 400, 300)
        self.show()

    def paintEvent(self, e):
        qp = QPainter()
        qp.begin(self)
        self.draw_roundedrect(qp)
        qp.end()

    def draw_roundedrect(self, qp: QPainter):
        qp.setPen(QPen(Qt.black, 3))

        qp.drawRoundedRect(20, 20, 100, 100, 0, 0)
        qp.drawText(40, 140, 'radius: 0')

        qp.drawRoundedRect(150, 20, 100, 100, 10, 10)
        qp.drawText(170, 140, 'radius: 10')

        qp.drawRoundedRect(280, 20, 100, 100, 20, 20)
        qp.drawText(300, 140, 'radius: 20')

        qp.drawRoundedRect(20, 160, 100, 100, 30, 30)
        qp.drawText(40, 280, 'radius: 30')

        qp.drawRoundedRect(150, 160, 100, 100, 40, 40)
        qp.drawText(170, 280, 'radius: 40')

        qp.drawRoundedRect(280, 160, 100, 100, 50, 50)
        qp.drawText(300, 280, 'radius: 50')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = MyApp()
    sys.exit(app.exec_())

50-2 설명

x-radius, y-radius 가 0, 0인 경우 직사각형이 되고, width, height의 절반인 경우 원(또는 타원)이 됩니다.

50-3 결과

50-4 예제2

import sys
from PyQt5.QtWidgets import (QApplication, QWidget)
from PyQt5.QtGui import (QPainter, QPen, QColor, QBrush)
from PyQt5.QtCore import Qt

class MyApp(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('drawRoundedRect')
        self.setGeometry(300, 300, 400, 300)
        self.show()

    def paintEvent(self, e):
        qp = QPainter()
        qp.begin(self)
        self.draw_roundedrect(qp)
        qp.end()

    def draw_roundedrect(self, qp: QPainter):
        brush = QBrush(Qt.SolidPattern)
        qp.setBrush(brush)
        qp.drawRoundedRect(20, 10, 100, 60, 15, 15)
        qp.drawText(20, 90, 'Qt.SolidPattern')

        brush = QBrush(Qt.Dense1Pattern)
        qp.setBrush(brush)
        qp.drawRoundedRect(150, 10, 100, 60, 15, 15)
        qp.drawText(150, 90, 'Qt.Dense1Pattern')

        brush = QBrush(Qt.Dense2Pattern)
        qp.setBrush(brush)
        qp.drawRoundedRect(280, 10, 100, 60, 15, 15)
        qp.drawText(280, 90, 'Qt.Dense2Pattern')

        brush = QBrush(Qt.HorPattern)
        qp.setBrush(brush)
        qp.drawRoundedRect(20, 110, 100, 60, 15, 15)
        qp.drawText(20, 190, 'Qt.HorPattern')

        brush = QBrush(Qt.VerPattern)
        qp.setBrush(brush)
        qp.drawRoundedRect(150, 110, 100, 60, 15, 15)
        qp.drawText(150, 190, 'Qt.VerPattern')

        brush = QBrush(Qt.CrossPattern)
        qp.setBrush(brush)
        qp.drawRoundedRect(280, 110, 100, 60, 15, 15)
        qp.drawText(280, 190, 'Qt.CrossPattern')

        brush = QBrush(Qt.BDiagPattern)
        qp.setBrush(brush)
        qp.drawRoundedRect(20, 210, 100, 60, 15, 15)
        qp.drawText(20, 290, 'Qt.BDiagPattern')

        brush = QBrush(Qt.FDiagPattern)
        qp.setBrush(brush)
        qp.drawRoundedRect(150, 210, 100, 60, 15, 15)
        qp.drawText(150, 290, 'Qt.FDiagPattern')

        brush = QBrush(Qt.DiagCrossPattern)
        qp.setBrush(brush)
        qp.drawRoundedRect(280, 210, 100, 60, 15, 15)
        qp.drawText(280, 290, 'Qt.DiagCrossPattern')
if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = MyApp()
    sys.exit(app.exec_())

50-5 설명

drawRect() 예제에서와 마찬가지로 QBrush() 와 setBrush()를 이용해서 둥근 직사각형의 채우기 패턴을 설정할 수 있습니다.

50-6 결과

'프로그래밍 > PyQt5 GUI' 카테고리의 다른 글

52. 타원 그리기 (drawEllipse)  (0) 2021.07.29
51. 다각형 그리기 (drawPolygon)  (0) 2021.07.29
49. 직사각형 그리고 (drawRect)  (0) 2021.07.29
48. 직선 그리고 (drawLine)  (0) 2021.07.29
47. 점 그리기 (drawPoint)  (0) 2021.07.29