PyQt5教程(九)——繪圖

PyQt5的繪圖系統可用於渲染矢量圖、圖像和文本。如果想改變或增強已有的控件,或者想從頭創建一個自定義控件時,我們就需要在程序中進行圖形的繪製。我們可以使用PyQt5提供的繪圖API進行繪圖操作。

繪圖要在paintEvent()方法中實現。在QPainter對象的begin()與end()方法間編寫繪圖代碼。它會在控件或其他圖形設備上進行低級的圖形繪製。

ps:我在後期使用pyqt繪圖的時候遇到無法實時刷新的問題,已解決,希望能幫助到遇到相同問題的人

self.repaint()

在需要重畫的地方調用QWidget的repaint方法即可,update方法有時不好用

繪製文本

我們先以窗體內Unicode文本的繪製爲例。

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


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

    def initUI(self):
        self.text = u'\u041b\u0435\u0432 \u041d\u0438\u043a\u043e\u043b\u0430\
\u0435\u0432\u0438\u0447 \u0422\u043e\u043b\u0441\u0442\u043e\u0439: \n\
\u0410\u043d\u043d\u0430 \u041a\u0430\u0440\u0435\u043d\u0438\u043d\u0430'

        self.setGeometry(300, 300, 280, 170)
        self.setWindowTitle("Draw text")
        self.show()

    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)


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

示例中我們繪製了一些西裏爾字母,文本是垂直且水平對齊的。

def paintEvent(self, event):
...

繪製工作在paintEvent的方法內部完成。

qp = QPainter()
qp.begin(self)
self.drawText(event, qp)
qp.end()

QPainter負責所有的低級繪製工作,在它的begin()與end()間放置了繪圖代碼。實際的繪製工作由drawText()方法完成。

qp.setPen(QColor(168, 34, 3))
qp.setFont(QFont('Decorative', 10))

這裏我們定義了用於繪製文本的畫筆與字體對象。

qp.drawText(event.rect(), Qt.AlignCenter, self.text)

drawText()會在窗體上進行文本的繪製。通過paint event(繪圖事件)的rect()方法得到當前窗體的可繪圖區域。

這裏寫圖片描述

繪製圓點

點是可以繪製的最簡單的圖形對象。

import sys, random
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtGui import QPainter, QColor, QPen
from PyQt5.QtCore import Qt


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

    def initUI(self):
        self.setGeometry(300, 300, 280, 170)
        self.setWindowTitle("Points")
        self.show()

    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()

        for i in range(1000): 
            x = random.randint(1, size.width()-1)
            y = random.randint(1, size.height()-1)
            qp.drawPoint(x, y)


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

示例中我們隨機畫了1000個紅點。

qp.setPen(Qt.red)

將畫筆設爲紅色。我們使用了預定義的Qt.red常量

size = self.size()

每次調整窗體尺寸都會生成一個paint event。我們可以通過這個event的size()方法得到窗體當前的尺寸。我們將這些點分配到窗體的各個區域。

qp.drawPoint(x, y)

通過drawPoint()方法繪製圓點。

這裏寫圖片描述

顏色

顏色是用於表示紅綠藍(RGB)各色值組合體的對象。合法的RGB值在0到255之間。顏色的定義有多種方式,通常用10進制或16進制的數值表示。我們也可以使用RGBA表示,它代表了紅色、綠色、藍色與Alpha通道值。也就是說我們附加了一個表示透明度的信息。Alpha值爲255表示完全不透明,爲0表示完全透明,也就是說顏色是不可見的。

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


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

    def initUI(self):
        self.setGeometry(300, 300, 350, 100)
        self.setWindowTitle("Colours")
        self.show()

    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)


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

示例中我們繪製了三個不同顏色的矩形。

color = QColor(0, 0, 0)
color.setNamedColor('#d4d4d4')

這裏我們使用16進制值定義了一個顏色對象。

qp.setBrush(QColor(200, 0, 0))
qp.drawRect(10, 15, 90, 60)

我們爲QPainter設置了一個筆刷(Bursh)對象並用它繪製了一個矩形。筆刷是用於繪製形狀背景的基本圖形對象。drawRect()方法接受四個參數,前兩個是起點的x,y座標,後兩個是矩形的寬和高。這個方法使用當前的畫筆與筆刷對象進行繪製。

這裏寫圖片描述

QPen(畫筆)

QPen是一個基本圖形對象。它用於繪製直線,曲線以及矩形、橢圓、多邊形或其他圖形的輪廓。

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


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

    def initUI(self):
        self.setGeometry(300, 300, 280, 270)
        self.setWindowTitle("Pen styles")
        self.show()

    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)


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

示例中我們繪製了6條直線。每條直線使用了不同的畫筆風格,其中有5個是PyQt5中預定義的,另外我們也自已實現了一個。最後那條線就是用我們自定義的畫筆風格所畫。

pen = QPen(Qt.black, 2, Qt.SolidLine)

我們創建了一個黑顏色的畫筆對象,其寬度爲2像素,這樣可以看出畫筆風格的不同之處。Qt.SolidLine是預定義的一種畫筆風格。

pen.setStyle(Qt.CustomDashLine)
pen.setDashPattern([1, 4, 5, 4])
qp.setPen(pen)

這裏我們定義了一個畫筆風格。我們設置了Qt.CustomDashLine並調用了setDashPattern()方法,它的參數(一個數字列表)定義了一種風格,必須有偶數個數字;其中奇數表示繪製實線,偶數表示留空。數值越大,直線或空白就越大。這裏我們定義了1像素的實線,4像素的空白,5像素實線,4像素空白。。。

這裏寫圖片描述

QBrush(筆刷)

QBrush是一個基本圖形對象。它用於繪製矩形、橢圓或多邊形等圖形的背景。筆刷有三種類型:預定義筆刷、漸變筆刷或紋理圖案筆刷。

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


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.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.Dense3Pattern)
        qp.setBrush(brush)
        qp.drawRect(10, 105, 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_())

在示例中我們繪製了9個不同的矩形。

brush = QBrush(Qt.SolidPattern)
qp.setBrush(brush)
qp.drawRect(10, 15, 90, 60)

我們定義了一個筆刷對象,然後將它設置給QPainter對象,並調用painter的drawRect()方法繪製矩形。

這裏寫圖片描述


在這部分教程中我們學習了一些基本的圖形繪製。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章