簡體   English   中英

PyQt5使用keyPressEvent()觸發paintEvent()

[英]PyQt5 triggering a paintEvent() with keyPressEvent()

我正在嘗試學習PyQt矢量繪畫。 目前,我一直在嘗試將信息傳遞給paintEvent()方法,我猜應該調用其他方法:

我正在嘗試將不同的數字繪制到基本塊(此處為drawFundBlock()方法,該方法應繪制一些線)。 該代碼試圖檢查是否按下了右箭頭-> drawFundamental塊,並且如果按下了數字(現在試圖簡單地繪制“ 5”),它將在該基本塊的某個區域上繪制該數字。 但是我似乎無法使QPainter正常工作。 似乎現在兩次調用paintEvent()覆蓋方法(為什么?)。 有人建議使用update()方法,但我不知道如何仍然將任何參數傳遞給paintEvent(),后者應確定是繪制“ fundblock”還是“ number”。 現在,代碼使用update()進行演示,但這只是移動了行-但已經添加的行應該保留!

有什么幫助嗎?

# Test QPainter etc.

from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtGui import QPainter, QPen, QColor, QFont
from PyQt5.QtCore import Qt, QPoint, pyqtSignal, QRect
import sys

class Example(QWidget):

    paintTrigger = pyqtSignal()

    def __init__(self):
        super().__init__()
        self.initUI()
        self.ydist = 15
        self.eveType = "drawBlock"
        self.currentRegion = QRect(50,50,50,80)
        #self.paintTrigger[self.eveType].connect(lambda:self.paintEvent())

        self.x0=5
        self.x1=25
        self.y0=5
        self.y1=25

    def initUI(self):
        self.setGeometry(300,300,280,270)
        self.setWindowTitle('Painter training')
        self.show()

    # How to pass info here, which type of drawing should be done (block or number)?
    def paintEvent(self,event):
        qp = QPainter(self)
        qp.begin(self)  
        self.drawFundBlock(qp)
        qp.end()

    def drawFundBlock(self,qp):
        pen = QPen(Qt.black, 2, Qt.SolidLine)
        pen.setStyle(Qt.DashLine)

        qp.setPen(pen)
        for i in range(1,10):
            #qp.drawLine(0,i*self.ydist,40,i*self.ydist)
            qp.drawLine(self.x0,i*self.y0,self.x1,self.y0*i)

        #notePoint=QPoint(200,200)
        #qp.drawText(notePoint,"5")

    def drawNumber(self,qp,notePoint):
        pen = QPen(Qt.black,2,Qt.SolidLine)
        #qp.setPen(QColor(200,200,200))
        qp.setPen(pen)
        qp.setFont(QFont('Arial', 10))
        qp.drawText(notePoint,"5")

    def nextRegion(self):
        self.x0=self.x0+30
        self.x1=self.x1+30
        self.y0=self.y0+30
        self.y1=self.y1+30

    def keyPressEvent(self,event):
        # Did the user press a button??
        gey=event.key()
        if gey == Qt.Key_M: 
            print("Key 'm' pressed!")
        elif gey == Qt.Key_Right:
            print("Right key pressed!, call drawFundBlock()")
            #self.paintTrigger["drawBlock"].emit()
            #self.paintEvent()
            self.update()
            self.nextRegion()

        elif gey == Qt.Key_5:
            print("#5 pressed, call drawNumber()")
            #self.paintTrigger["drawNo"].emit()


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

QPaintEvent不應直接調用,我們必須通過update() ,這將在必要時進行內部調用。

每次調用QPaintEvent時, QPaintEvent在此干凈的空間中進行繪制,因此不會節省以前的圖形的內存,一種簡單的解決方案是首先繪制一個QPixmap ,它將存儲您先前繪制的內容,然后使用那個QPixmap

另一件事是,以下2條指令是等效的:

1。


painter = QPainter(some_QPaintDevice)

2。


painter = QPainter()
painter.begin(some_QPaintDevice)

這兩種方法都將對象傳遞到將要繪制對象的位置,在您的情況下,您將分配相同的小部件2倍。

為了方便繪圖,我提出了方法drawBackground ,該方法需要用self.func填充,第一個參數必須是函數的名稱,第二個參數是具有字典的字典,除了QPainter之外,還需要其他參數。

class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.mModified = True
        self.initUI()
        self.currentRegion = QRect(50, 50, 50, 80)
        self.x0 = 5
        self.x1 = 25
        self.y0 = 5
        self.y1 = 25
        self.mPixmap = QPixmap()
        self.func = (None, None)

    def initUI(self):
        self.setGeometry(300, 300, 280, 270)
        self.setWindowTitle('Painter training')
        self.show()

    def paintEvent(self, event):
        if self.mModified:
            pixmap = QPixmap(self.size())
            pixmap.fill(Qt.white)
            painter = QPainter(pixmap)
            painter.drawPixmap(0, 0, self.mPixmap)
            self.drawBackground(painter)
            self.mPixmap = pixmap
            self.mModified = False

        qp = QPainter(self)
        qp.drawPixmap(0, 0, self.mPixmap)

    def drawBackground(self, qp):
        func, kwargs = self.func
        if func is not None:
            kwargs["qp"] = qp
            func(**kwargs)

    def drawFundBlock(self, qp):
        pen = QPen(Qt.black, 2, Qt.SolidLine)
        pen.setStyle(Qt.DashLine)

        qp.setPen(pen)
        for i in range(1, 10):
            qp.drawLine(self.x0, i * self.y0, self.x1, self.y0 * i)

    def drawNumber(self, qp, notePoint):
        pen = QPen(Qt.black, 2, Qt.SolidLine)
        qp.setPen(pen)
        qp.setFont(QFont('Arial', 10))
        qp.drawText(notePoint, "5")

    def nextRegion(self):
        self.x0 += 30
        self.x1 += 30
        self.y0 += 30
        self.y1 += 30

    def keyPressEvent(self, event):
        gey = event.key()
        self.func = (None, None)
        if gey == Qt.Key_M:
            print("Key 'm' pressed!")
        elif gey == Qt.Key_Right:
            print("Right key pressed!, call drawFundBlock()")
            self.func = (self.drawFundBlock, {})
            self.mModified = True
            self.update()
            self.nextRegion()
        elif gey == Qt.Key_5:
            print("#5 pressed, call drawNumber()")
            self.func = (self.drawNumber, {"notePoint": QPoint(100, 100)})
            self.mModified = True
            self.update()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM