大阪市中央区 システムソフトウェア開発会社

営業時間:平日09:15〜18:15
MENU

ラズパイデスクトップでPyQt5入門(21)Painting in PyQt5(3)

著者:國松亜紗子
公開日:2020/06/26
最終更新日:2020/06/26
カテゴリー:技術情報

こんばんは。湿度が高いとテンション下がる系の國松です。

参考サイト
ZetCode PyQt5 tutorial
【PythonでGUI】PyQt5 -始めの一歩-

5.QBrush(塗りつぶし)
QBrushは長方形、楕円、多角形などのグラフィック形状の背景をペイントするために使用します。
ブラシには定義済みのブラシ、グラデーション、テクスチャパターンの3種類があります。
今回は背景が異なる9個の長方形を描画していきたいと思います。

#!/usr/bin/python3
#-*-coding:utf-8-*-

"""
This example draws nine rectangles in different brush styles
"""

from PyQt5.QtWidgets import QWidget,QApplication
from PyQt5.QtGui import QPainter,QBrush
from PyQt5.QtCore import Qt
import sys

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

    def initUI(self):
        self.setGeometry(300,300,355,280)
        self.setWindowTitle('Brushes')
        self.show()
        
    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 = QBrush(Qt.Dense1Pattern)
        qp.setBrush(brush)
        qp.drawRect(130,15,90,60)

        brush = QBrush(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)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())





brush.QVrush(Qt.SolidPattern) ブラシオブジェクトを定義
qp.setBrush(brush) ペイントオブジェクトにブラシを設定
qp.drawRect(10,15,90,60) 長方形を描画して塗りつぶす

6.Bezier Curve(ベジェ曲線)。
ベジェ曲線とは?

N個の制御点から得られるN-1次曲線の事です。どんな曲線か直ぐにイメージできたあなたはすごい。
ざっくり言えばCG等において滑らかな曲線を描くためによく利用される曲線です。(ざっくりしすぎですみません)

PyQt5のベジエ曲線はQPainterPathで作成できます。QPainterPathは長方形、楕円、線、曲線など多くのグラフィカルなビルディングブロックで構成させるオブジェクトです。

#!/usr/bin/python3
#-*-coding:utf-8-*-

"""
This program draws a bezier curve eith QPainterPath
"""

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

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

   def initUI(self):
      self.setGeometry(300,300,380,250)
      self.setWindowTitle('Bezier curve')
      self.show()

   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)

if __name__ == '__main__':
   app = QApplication(sys.argv)
   ex = Example()
   sys.exit(app.exec_())



Path = QPainterPath() //QPainterPathを使用してベジェ曲線を作成
Path.move(30,30)
Path.cubicto(30,30,200,250,350,30)  //30,30が開始点、200,350が制御点、350,30が終了点
draw.path()で描画

今回はここまでになります。それではまた。

    上に戻る