[英]How to add an argument to pyqt5 paintEvent?
在paintEvent中运行的函数需要将multiprocessing.Queue对象传递给自身。
我曾尝试使用全局python列表,但列表不适用于多处理库。 在我的代码的“main”部分,我创建了一个multiprocess.Queue对象。 函数drawMandelbrot是我的QWidget类的一部分,由paintEvent执行。 只要需要在屏幕上绘制gui窗口,就会运行paint事件。 但是函数drawMandelbrot需要访问Queue对象以获取需要绘制的数据。
if __name__ == '__main__':
procQueue = Queue()
app = QApplication([])
#Called whenever the window is resized or brought into focus
def paintEvent(self, event, procQueue):
qp = QPainter()
qp.begin(self)
#Run the drawMandelbrot program
self.drawMandelbrot(qp, procQueue)
qp.end()
我希望函数将Queue对象传递给drawMandelbrot函数。 当程序运行时,它会给出错误“TypeError:paintEvent()缺少1个必需的位置参数:'Queue'”。 如何允许drawMandelbrot函数访问我在python应用程序的“main”部分中创建的Queue对象?
您无法修改继承的类的方法的签名,因此在这些情况下的解决方案是通过类的属性传递变量:
class FooWidget(QWidget):
def __init__(self, q, parent=None):
super(FooWidget, self).__init__(parent)
self.m_q = q
def paintEvent(self, event):
painter = QPainter(self)
self.drawMandelbrot(painter, self.m_q)
def drawMandelbrot(sef, painter, q):
# ... some code
if __name__ == '__main__':
procQueue = Queue()
app = QApplication([])
w = FooWidget(procQueue)
# ...
另一方面,paintEvent(显然是drawMandelbrot)只能在主进程的主线程中执行,因为Qt不支持多处理,并且GUI必须在作为主线程的GUI线程中执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.