Skip to content

Instantly share code, notes, and snippets.

@peace098beat
Created July 2, 2015 03:34
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save peace098beat/29e0f0ac1bb3d690e1e4 to your computer and use it in GitHub Desktop.
[PySide] 線を引く
# coding: utf-8
# ***************************************************
#
# GraphWidget.py
#
# ***************************************************
import sys
import numpy as np
from PySide import QtCore, QtGui
class GraphWidget(QtGui.QWidget):
def paintEvent(self, event):
""" 絵を描く
:link: http://zetcode.com/gui/pysidetutorial/drawing/
"""
# QPainterの生成
# :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
painter = QtGui.QPainter()
# 描画開始 (おまじない)
painter.begin(self)
# 描画する関数を呼び出す
# --------------------------
self.settingLine(painter)
# 描画終了 (おまじない)
painter.end()
def settingLine(self, painter):
""" ラインを引く
ラインを引くためには、QPenオブジェクトを生成し、設定を行う
:link: https://srinikom.github.io/pyside-docs/PySide/QtGui/QPen.html
:link: http://zetcode.com/gui/pysidetutorial/drawing/
"""
# QPenの生成
# :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
pen = QtGui.QPen(QtCore.Qt.black, 2, QtCore.Qt.SolidLine)
# 線の太さの設定
pen.setWidth(3)
# Cap Style 線の端部の形状変更
pen.setCapStyle(QtCore.Qt.SquareCap) # 四角
pen.setCapStyle(QtCore.Qt.FlatCap) # 四角
pen.setCapStyle(QtCore.Qt.RoundCap) # 丸
# Join Style: 線の角部の形状
pen.setJoinStyle(QtCore.Qt.BevelJoin) # フラット
pen.setJoinStyle(QtCore.Qt.MiterJoin) # 角
pen.setJoinStyle(QtCore.Qt.RoundJoin) # 丸
# 線の種類の変更
pen.setStyle(QtCore.Qt.SolidLine) # ソリッド
pen.setStyle(QtCore.Qt.DashLine) # 破線
pen.setStyle(QtCore.Qt.DotLine) # 点線
pen.setStyle(QtCore.Qt.DashDotLine) # 1点破線
pen.setStyle(QtCore.Qt.DashDotDotLine) # 2点は線
pen.setStyle(QtCore.Qt.CustomDashLine) # ユーザ指定
pen.setDashPattern([1, 4, 5, 4])
# プロパティの適用 (おまじない)
painter.setPen(pen)
# drawLine(int,int,int,int)
painter.drawLine(0, np.ceil(self.height() / 2), self.width(), np.ceil(self.height() / 2))
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = GraphWidget()
window.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment