HomeAboutMeBlogGuest
© 2025 Sejin Cha. All rights reserved.
Built with Next.js, deployed on Vercel
📟
PyQt 5 Lecture
/
🍊
004 QBrush (도형 그리기 - 1)
🍊

004 QBrush (도형 그리기 - 1)

1. 코드2. 상세 내용3. 실행 화면

1. 코드

import sys from PyQt5.QtWidgets import QApplication, QWidget from PyQt5.QtGui import QPainter, QColor, QFont, QPen, QBrush from PyQt5.QtCore import Qt class 도형그리기(QWidget): def __init__(self): super().__init__() self.UI초기화() def UI초기화(self): self.setGeometry(300, 300, 500, 500) self.setWindowTitle('도형 그리기!') self.show() def paintEvent(self, event): paint = QPainter() paint.begin(self) self.drawFigure(paint) paint.end() def drawFigure(self, paint): paint.setBrush(QColor(10, 255, 40)) paint.setPen(QPen(QColor(Qt.red))) paint.drawRect(20, 30, 100, 100) paint.setBrush(QColor(10, 255, 40)) paint.setPen(QPen(QColor(Qt.red))) paint.drawRoundedRect(150, 20, 100, 100, 30, 30) paint.setBrush(QBrush(Qt.CrossPattern)) paint.drawRoundedRect(300, 100, 100, 100, 30, 30) paint.setBrush(QColor(Qt.darkGreen)) paint.setPen(QPen(QColor(Qt.red), 2, Qt.DotLine)) paint.drawEllipse(150, 200, 180, 220) 프로그램무한반복 = QApplication(sys.argv) 실행인스턴스 = 도형그리기() 프로그램무한반복.exec_()
 

2. 상세 내용

아래 코드를 보시면 QBrush를 통해 각각 도형의 패턴을 입히고 있는 것을 볼 수 있습니다. 또한 QPen에 QColor안에 들어가는 컬러 이름이 Qt에 내장되어 있다는 사실도 기억하고 가시면 좋을 것 같습니다.
paint.setPen(QPen(QColor(Qt.red)) #... paint.setBrush(QBrush(Qt.CrossPattern))
  • QPainter : 그림판 같은 역할을 하는 모듈입니다.
  • QColor : Color와 관련된 모듈입니다. 0부터 255 값으로 된 RGB 값으로 줄 수도 있고, color name으로도 줄 수 있으며, hex값으로도 줄 수 있습니다. color에 대한 기본적인 개념을 잡고 싶으신 분은 아래 문서를 참고하세요.
HTML Color Codes
Find that perfect color with our color picker and discover beautiful color harmonies, tints, shades and tones; input Hex color codes, RGB and HSL values, and generate HTML, CSS and SCSS styles. Looking for some already great color combinations?
HTML Color Codes
https://htmlcolorcodes.com/
HTML Color Codes
QColor - Qt for Python
A color is normally specified in terms of RGB (red, green, and blue) components, but it is also possible to specify it in terms of HSV (hue, saturation, and value) and CMYK (cyan, magenta, yellow and black) components. In addition a color can be specified using a color name.
QColor - Qt for Python
https://doc.qt.io/qtforpython/PySide2/QtGui/QColor.html
QColor - Qt for Python
  • QFont : 폰트와 같은 설정을 할 수 있습니다. 굵기와 폰트 스타일 등을 지정할 수 있습니다. 상세 설정은 공식문서를 참고바랍니다.
QFont - Qt for Python
When you create a object you specify various attributes that you want the font to have. Qt will use the font with the specified attributes, or if no matching font exists, Qt will use the closest matching installed font. The attributes of the font that is actually used are retrievable from a object.
QFont - Qt for Python
https://doc.qt.io/qtforpython/PySide2/QtGui/QFont.html
  • QPen : 그림판의 펜과 같은 역할을 합니다.
QPen - Qt for Python
The pen style defines the line type. The brush is used to fill strokes generated with the pen. Use the class to specify fill styles. The cap style determines the line end caps that can be drawn using , while the join style describes how joins between two lines are drawn.
QPen - Qt for Python
https://doc.qt.io/qtforpython/PySide2/QtGui/QPen.html
QPen - Qt for Python
  • QBrush : 패턴을 채울 수 있습니다. 디자인 패턴 이름은 아래 공식 문서를 참고해주세요.
QBrush - Qt for Python
A brush has a style, a color, a gradient and a texture. The brush defines the fill pattern using the BrushStyle enum. The default brush style is (depending on how you construct a brush). This style tells the painter to not fill shapes. The standard style for filling is SolidPattern .
QBrush - Qt for Python
https://doc.qt.io/qtforpython/PySide2/QtGui/QBrush.html
QBrush - Qt for Python

3. 실행 화면

 
notion image