pythonpyqt4paintevent

Calling paintEvent() from a class in pyqt4 python


I have written a class to display rectangles (the cell class). I would like to have functions inside the class to call in another class (i.e. calling cell.paintEvent(self,event) and cell.drawRectangles(self,qp) in a function defined in a Window class). Unfortunately, I do not know how to call these functions in another class (i.e. Window) as they both require arguments (i.e. event and pq) and I do not know what to pass on to them.

Here is the code for my cell class:

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)

Here is the part of the code (specifically def.initiate(self)) where I want to instantiate an array of cell objects ( which I can easily do) and then call its relevant display functions (i.e. cell.paintEvent(self,event) and cell.drawRectangles(self,qp), which I still have not figured out how to do):

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)

Solution

  • The paintEvent method must be overwritten by the classes that inherit from QWidget. You can implement the function drawRectangles but you must invoke the paintEvent method using update() method.

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

    enter image description here