Python Qt UI即使在使用线程后也响应缓慢

Python Qt UI即使在使用线程后也响应缓慢,python,multithreading,pyside,Python,Multithreading,Pyside,我正在尝试使用PySide构建一个图像/视频处理应用程序,并一直试图在图像处理过程中保持UI的响应性。然而,尽管使用了线程,UI对输入的响应仍然很慢。任何关于我做错了什么的建议都会很有帮助 class Communicate(QObject): sendPixmap = Signal(QPixmap) def __init__(self): QObject.__init__(self) class UpdateThread(QThread): def _

我正在尝试使用PySide构建一个图像/视频处理应用程序,并一直试图在图像处理过程中保持UI的响应性。然而,尽管使用了线程,UI对输入的响应仍然很慢。任何关于我做错了什么的建议都会很有帮助

class Communicate(QObject):
    sendPixmap = Signal(QPixmap)
    def __init__(self):
        QObject.__init__(self)

class UpdateThread(QThread):
    def __init__(self, parent):
        QThread.__init__(self, parent)
        self.parent = parent
        self.signals = Communicate()
        self.signals.sendPixmap.connect(parent.receiveFrame)

    def run(self):
        print("[INFO] updateFrame called")
        while window.threadFlag:
            print("Processing")
            start = time.time()
            qtImg = videoController.nextQtFrame()
            pixmap = QPixmap(qtImg)
            self.signals.sendPixmap.emit(pixmap.scaled(self.parent.ui.imgDisplayLabel.size(),
                                                    Qt.KeepAspectRatio,
                                                    Qt.SmoothTransformation))
            end = time.time()
            print("Time taken: ", end - start)
        print("[INFO] update frame finished")

class MainWindow(QMainWindow):
    def __init__(self):
        print("Initialising Main Window")
        start = time.time()
        super(MainWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.threadFlag = True
        self.thread = UpdateThread(self)
        self.thread.start()
        end = time.time()
        print("Time taken: ",end - start)

    @Slot(QPixmap)
    def receiveFrame(self, pixmap):
        print("Receiving and displaying frame")
        start = time.time()
        print("[INFO] receiveFrame Slot invoked!")
        self.ui.imgDisplayLabel.setPixmap(pixmap)
        end = time.time()
        print("Time taken: ",end - start)


    def closeEvent(self, event:QCloseEvent):
        # Closing the video capture thread
        videoController.threadFlag =False
        self.threadFlag = False
        self.thread.wait()
        super(MainWindow, self).closeEvent(event)
        app.exit()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    app.setQuitOnLastWindowClosed(True)
    videoController = VideoController()
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

不要在次线程中使用QPixmap,而必须使用QImage。另一方面,使用次线程不能提高速度,但不会阻止其他元素。如果你想更快,那么你应该让你的处理更有效,在几毫秒内阻止我的元素。我试过使用QImage,但是没有用。