簡體   English   中英

如何在pyqt5 paintEvent中添加參數?

[英]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.

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