簡體   English   中英

從pyqt4 python中的類調用paintEvent()

[英]Calling paintEvent() from a class in pyqt4 python

我寫了一個類來顯示矩形(單元類)。 我希望類中的函數在另一個類中調用(即在 Window 類中定義的函數中調用cell.paintEvent(self,event)cell.drawRectangles(self,qp) )。 不幸的是,我不知道如何在另一個類(即 Window)中調用這些函數,因為它們都需要參數(即eventpq ),而且我不知道要傳遞給它們什么。

這是我的單元格類的代碼:

class cell(object):
    def __init__(self, c, x, y, w, h, active,flux_val,index):

        self.c1 = c
        self.c2 = c
        self.c3 = 255
        self.x = x
        self.y = y
        self.w = w
        self.h = h
        self.index = index
        self.active = active
        self.flux_val = flux_val
        self.isChecked = False
        self.isHit = False

    def paintEvent(self, e):

        qp = QtGui.QPainter()
        qp.begin(self)
        self.drawRectangles(qp)
        qp.end()

    def drawRectangles(self, qp):

        color = self.c2
        #qp.setPen(color)

        qp.setBrush(color)
        qp.drawRect(self.x, self.y, self.w, self.h)

這是代碼的一部分(特別是def.initiate(self) ),我想在其中實例化一個單元格對象數組(我很容易做到),然后調用其相關的顯示函數(即cell.paintEvent(self,event)cell.drawRectangles(self,qp) ,我還沒有想出怎么做):

import sys
from PyQt4 import QtGui, QtCore
import numpy as np


class Window(QtGui.QMainWindow):
    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(50, 50, 1000, 800)
        self.setWindowTitle("PyQT tuts!")
        self.initiate()
        self.show()

    def initiate(self):
        #initiate an array of cell objects
        #Call their display functions (or any other relevant class functions)

paintEvent方法必須被繼承自 QWidget 的類覆蓋。 您可以實現drawRectangles函數,但必須使用update()方法調用paintEvent方法。

import sys
from PyQt4 import QtGui, QtCore


class cell(object):
    def __init__(self, c, x, y, w, h):
        self.color = c
        self.x = x
        self.y = y
        self.w = w
        self.h = h

    def drawRectangles(self, qp):
        qp.setBrush(self.color)
        qp.drawRect(self.x, self.y, self.w, self.h)


class Window(QtGui.QMainWindow):
    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(50, 50, 1000, 800)
        self.setWindowTitle("PyQT tuts!")
        self.cells = []

        now = QtCore.QTime.currentTime()
        QtCore.qsrand(now.msec())
        self.createCells()

    def createCells(self):
        for i in range(100):
            self.cells.append(cell(QtGui.QColor(QtCore.qrand() % 256,
                                                QtCore.qrand() % 256,
                                                QtCore.qrand() % 256),
                                   QtCore.qrand() % self.width(), QtCore.qrand() % self.height(),
                                   QtCore.qrand() % 40, QtCore.qrand() % 40))
        self.update()

    def paintEvent(self, e):
        qp = QtGui.QPainter(self)
        for c in self.cells:
            c.drawRectangles(qp)


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    w = Window()
    w.show()
    sys.exit(app.exec_())

在此處輸入圖片說明

那么,您需要導入單元格對象。 為此你需要

from yourFileNameWithCellObject import cell

要初始化一個單元格,你只需要做

newCell = cell(args_here)

或將newCell作為self (Window) 的一部分

self.newCell = cell(args_here)

在單元格對象中調用方法非常簡單。

self.newCell.paintEvent(args_here)

暫無
暫無

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

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