本文共 6654 字,大约阅读时间需要 22 分钟。
PyQt5提供了强大的绘画功能,能够渲染矢量图形、图像以及基于轮廓字体的文本。无论是对现有小部件进行修改,还是创建自定义小部件,应用程序都需要绘图支持。PyQt5提供了丰富的绘图API,能够满足各种绘图需求。
QPainter是PyQt5中用于绘制的核心工具。它能够在小部件和其他绘画设备上执行低级绘图操作。无论是绘制简单的线条,还是复杂的图形,QPainter都能胜任。它通常在窗口的绘图事件paintEvent()中使用。
QPainter的使用流程如下:
paintEvent()方法中调用begin()方法启动绘图。end()方法完成绘图。repaint()或update()方法触发下一次绘图。绘图工作始终在paintEvent()方法中执行。QPainter的所有绘图方法需要在begin()和end()之间进行。具体来说:
def paintEvent(self, event): qp = QPainter() qp.begin(self) self.drawText(event, qp) qp.end()
要在窗口中绘制文本,可以参考以下代码示例:
import sysfrom PyQt5.QtWidgets import QWidget, QApplicationfrom PyQt5.QtGui import QPainter, QColor, QFontfrom PyQt5.QtCore import Qtclass Example(QWidget): def __init__(self): super().__init__() self.text = "Лев Николаевич Толстой\nАнна Каренина" self.setGeometry(300, 300, 350, 300) self.setWindowTitle('Drawing text') def paintEvent(self, event): qp = QPainter() qp.begin(self) self.drawText(event, qp) qp.end() def drawText(self, event, qp): qp.setPen(QColor(168, 34, 3)) qp.setFont(QFont('Decorative', 10)) qp.drawText(event.rect(), Qt.AlignCenter, self.text)def main(): app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())if __name__ == '__main__': main() 要绘制多个随机点,可以参考以下代码示例:
import sysfrom PyQt5.QtWidgets import QWidget, QApplicationfrom PyQt5.QtGui import QPainterfrom PyQt5.QtCore import Qtclass Example(QWidget): def __init__(self): super().__init__() self.setGeometry(300, 300, 300, 190) self.setWindowTitle('Points') def paintEvent(self, e): qp = QPainter() qp.begin(self) self.drawPoints(qp) qp.end() def drawPoints(self, qp): qp.setPen(Qt.red) size = self.size() if size.height() <= 1: return for i in range(1000): x = random.randint(1, size.width() - 1) y = random.randint(1, size.height() - 1) qp.drawPoint(x, y)def main(): app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())if __name__ == '__main__': main() PyQt5支持多种颜色表示方式,包括RGB和RGBA值。以下代码示例展示了如何使用不同的颜色来绘制矩形:
import sysfrom PyQt5.QtWidgets import QWidget, QApplicationfrom PyQt5.QtGui import QPainter, QColor, QBrushimport sysclass Example(QWidget): def __init__(self): super().__init__() self.setGeometry(300, 300, 350, 100) self.setWindowTitle('Colours') def paintEvent(self, e): qp = QPainter() qp.begin(self) self.drawRectangles(qp) qp.end() def drawRectangles(self, qp): col = QColor(0, 0, 0) col.setNamedColor('#d4d4d4') qp.setPen(col) qp.setBrush(QColor(200, 0, 0)) qp.drawRect(10, 15, 90, 60) qp.setBrush(QColor(255, 80, 0, 160)) qp.drawRect(130, 15, 90, 60) qp.setBrush(QColor(25, 0, 90, 200)) qp.drawRect(250, 15, 90, 60)def main(): app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())if __name__ == '__main__': main() QPen是PyQt5中用于绘制图形边缘的工具。以下代码示例展示了如何使用不同笔样式绘制线条:
import sysfrom PyQt5.QtWidgets import QWidget, QApplicationfrom PyQt5.QtGui import QPainter, QPenfrom PyQt5.QtCore import Qtclass Example(QWidget): def __init__(self): super().__init__() self.setGeometry(300, 300, 280, 270) self.setWindowTitle('Pen styles') def paintEvent(self, e): qp = QPainter() qp.begin(self) self.drawLines(qp) qp.end() def drawLines(self, qp): pen = QPen(Qt.black, 2, Qt.SolidLine) qp.setPen(pen) qp.drawLine(20, 40, 250, 40) pen.setStyle(Qt.DashLine) qp.setPen(pen) qp.drawLine(20, 80, 250, 80) pen.setStyle(Qt.DashDotLine) qp.setPen(pen) qp.drawLine(20, 120, 250, 120) pen.setStyle(Qt.DotLine) qp.setPen(pen) qp.drawLine(20, 160, 250, 160) pen.setStyle(Qt.DashDotDotLine) qp.setPen(pen) qp.drawLine(20, 200, 250, 200) pen.setStyle(Qt.CustomDashLine) pen.setDashPattern([1, 4, 5, 4]) qp.setPen(pen) qp.drawLine(20, 240, 250, 240)def main(): app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())if __name__ == '__main__': main() QBrush是PyQt5中用于绘制图形内部的工具。以下代码示例展示了如何使用不同画刷风格绘制矩形:
import sysfrom PyQt5.QtWidgets import QWidget, QApplicationfrom PyQt5.QtGui import QPainter, QBrushfrom PyQt5.QtCore import Qtclass Example(QWidget): def __init__(self): super().__init__() self.setGeometry(300, 300, 355, 280) self.setWindowTitle('Brushes') def paintEvent(self, e): qp = QPainter() qp.begin(self) self.drawBrushes(qp) qp.end() def drawBrushes(self, qp): brush = QBrush(Qt.SolidPattern) qp.setBrush(brush) qp.drawRect(10, 15, 90, 60) brush.setStyle(Qt.Dense1Pattern) qp.setBrush(brush) qp.drawRect(130, 15, 90, 60) brush.setStyle(Qt.Dense2Pattern) qp.setBrush(brush) qp.drawRect(250, 15, 90, 60) brush.setStyle(Qt.DiagCrossPattern) qp.setBrush(brush) qp.drawRect(10, 105, 90, 60) brush.setStyle(Qt.Dense5Pattern) qp.setBrush(brush) qp.drawRect(130, 105, 90, 60) brush.setStyle(Qt.Dense6Pattern) qp.setBrush(brush) qp.drawRect(250, 105, 90, 60) brush.setStyle(Qt.HorPattern) qp.setBrush(brush) qp.drawRect(10, 195, 90, 60) brush.setStyle(Qt.VerPattern) qp.setBrush(brush) qp.drawRect(130, 195, 90, 60) brush.setStyle(Qt.BDiagPattern) qp.setBrush(brush) qp.drawRect(250, 195, 90, 60)def main(): app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())if __name__ == '__main__': main() PyQt5支持绘制贝塞尔曲线。以下代码示例展示了如何使用QPainterPath绘制贝塞尔曲线:
import sysfrom PyQt5.QtGui import QPainter, QPainterPathfrom PyQt5.QtWidgets import QWidget, QApplicationclass Example(QWidget): def __init__(self): super().__init__() self.setGeometry(300, 300, 380, 250) self.setWindowTitle('Bézier curve') def paintEvent(self, e): qp = QPainter() qp.begin(self) qp.setRenderHint(QPainter.Antialiasing) self.drawBezierCurve(qp) qp.end() def drawBezierCurve(self, qp): path = QPainterPath() path.moveTo(30, 30) path.cubicTo(30, 30, 200, 350, 350, 30) qp.drawPath(path)def main(): app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())if __name__ == '__main__': main() 转载地址:http://ucafk.baihongyu.com/