2016-12-13 22 views
1

调用从一个类的paintEvent()我已经写了类来显示矩形(细胞类)。我想有里面的类的功能在另一个类中调用(即调用在一个窗口类中定义的函数cell.paintEvent(self,event)cell.drawRectangles(self,qp))。不幸的是,我不知道如何在另一个类(即窗口)调用这些函数,因为它们都需要参数(即eventpq),我不知道该怎么传授给他们。PyQt4中蟒

这里是我的细胞类的代码:

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) 

回答

1

paintEvent方法必须被继承自QWidget的类所覆盖。您可以实现功能drawRectangles,但您必须在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_()) 

enter image description here

-1

那么,你就需要导入单元对象。为此,你需要

from yourFileNameWithCellObject import cell 

要初始化一个细胞,你只需要做

newCell = cell(args_here) 

或使newCellself(窗口)的一部分

self.newCell = cell(args_here) 

要单元格对象中的调用方法非常简单。

self.newCell.paintEvent(args_here) 
+0

其实所有的代码是在一个文件中。我已经定义了一个单元对象(即test = cell(a,b,c,...)),然后在initiate()中尝试了test.paintEvent()。但我提示paintEvent()需要一个参数,它不起作用。当我想通过一个'事件'的论点时,我不知道该怎么办。我确实定义了一个函数,它是名为def pr(self)的单元类,它只会打印一个字符串。我可以很容易地调用该函数(即test.pr(),将打印字符串)。可能如果paintEvent是一个void,我只能说test.paintEvent(),它会为我绘制形状。 –

+0

@Jamycodes好,如果你有一个叫做功能'pr',然后通过在事件参数'pr'像'cell.paintEvent(cell.pr)'。但是从上面发布的代码中,我看不到'paintEvent(self,e)'中的'e'在被使用。 –

+0

这是一个很好的观点。我也看不到它。我从这个使用相同函数的例子中领先,但没有像我这样的对象类。我看不到的地方的“E”作为参数是在函数被inputed: 这里是链接: [http://zetcode.com/gui/pyqt4/drawing/] –