从pyqt4 python中的类调用paintEvent()
我已经编写了一个显示矩形的类(cell类)。我希望在类中有函数在另一个类中调用(即在窗口类中定义的函数中调用从pyqt4 python中的类调用paintEvent(),python,pyqt4,paintevent,Python,Pyqt4,Paintevent,我已经编写了一个显示矩形的类(cell类)。我希望在类中有函数在另一个类中调用(即在窗口类中定义的函数中调用cell.paintEvent(self,event)和cell.draw矩形(self,qp))。不幸的是,我不知道如何在另一个类(即窗口)中调用这些函数,因为它们都需要参数(即event和pq),我不知道传递给它们什么 以下是my cell类的代码: class cell(object): def __init__(self, c, x, y, w, h, active,flu
cell.paintEvent(self,event)
和cell.draw矩形(self,qp)
)。不幸的是,我不知道如何在另一个类(即窗口)中调用这些函数,因为它们都需要参数(即event
和pq
),我不知道传递给它们什么
以下是my cell类的代码:
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.draw矩形(self,qp)
,我还没有弄清楚该怎么做):
嗯,您需要导入单元格对象。要做到这一点,你需要
from yourFileNameWithCellObject import cell
要初始化单元格,只需执行以下操作
newCell = cell(args_here)
或使新单元格
成为自身
(窗口)的一部分
在cell对象中调用方法非常简单
self.newCell.paintEvent(args_here)
paintEvent
方法必须被从QWidget继承的类覆盖。您可以实现函数draw矩形
,但必须使用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_())
实际上,所有代码都在一个文件中。我已经定义了一个单元格对象(即test=cell(a,b,c,…),然后在initiate()中尝试了test.paintEvent()。但是我被提示paintEvent()需要一个参数,它不起作用。我不知道当我应该传递一个“事件”参数时该怎么办。我确实定义了一个名为def pr(self)的cell类函数,它只打印一个字符串。我可以很容易地调用该函数(即test.pr(),将打印字符串)。可能如果paintEvent是空的,我可以说test.paintEvent(),它会为我绘制形状。@Jamycodes如果你有一个名为
pr
的函数,那么传入事件参数pr
就像cell.paintEvent(cell.pr)
。但是从你上面发布的代码来看,我看不出paintEvent(self,e)
中的e
在哪里被使用。这是一个好的观点。我也看不见。我从这个示例中学习,它使用相同的函数,但没有像我这样的对象类。我看不到函数中“e”作为参数输入的位置:这是链接:[很抱歉回复太晚和链接不好。提供的答案是正确的,尽管如此,我正在重新发布链接并确保它没有断开:
self.newCell.paintEvent(args_here)
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_())