PySide和QProgressBar在不同的线程中更新


问题内容

这可能是篇幅较长的文章,因此,感谢您一直陪伴我直到最后。这里的问题是(我认为这是一个基本的问题,只是我对PiSide和Qt的经验不足,这使我更难了。)我有一个带有一个菜单项的主窗口,假设为“
Process”。代码如下-

from PySide import QtCore, QtGui

class Ui_MainWindow(object):

      def setupUi(self, MainWindow, AppObj):
            .
            .
            self.statusbar = QtGui.QStatusBar(MainWindow)
            self.statusbar.setObjectName("statusbar")
            MainWindow.setStatusBar(self.statusbar)
            .
            .
            .
           self.actionProcess = QtGui.QAction(MainWindow)
           self.actionProcess.setObjectName("actionProcess")
           self.actionProcess.triggered.connect(self.myappObj.menuActionProcess) 
           .

这里self.myappobj指的是我创建的一个应用程序类,它充当我的应用程序的主要逻辑控制器。编码 -

from PySide import QtCore, QtGui
from MainWindow import Ui_MainWindow

class App(QtGui.QDialog):
      def __init__(self, parent=None):
          self.__mainWindow = QtGui.QMainWindow()
          self.__mainWindowDesignContext = Ui_MainWindow()
          self.__mainWindowDesignContext.setupUi(self.__mainWindow, self)
          self.__mainWindow.show()
      def menuActionProcess(self):
          self.processThread = BatchProcesser()
          self.progressBar = QtGui.QProgressBar()
          statusBar.addWidget(self.progressBar)
          self.progressBar.show()
          self.progressBar.setMinimum(0)
          self.progressBar.setMaximum(100)
          QtCore.QObject.connect(self.processThread, QtCore.SIGNAL("progress(int)"),self.progressBar, QtCore.SLOT("setValue(int)"), QtCore.Qt.DirectConnection)
          if not self.processThread.isRunning():
              self.processThread.exiting = False
              self.processThread.start()

因此,很容易看出我在这里要做的是创建一个主窗口。在其中添加一个名为“
Process”的菜单,单击该菜单应触发应用程序类中的回调menuActionProcess方法,在其中创建一个进度条,并将其附加到主窗口的状态栏(实际代码还有很多其他内容,我在这里给出的是重新排列为伪示例的必要部分),最后在上述代码中提到的BatchProcesser类中,我正在这样做-

from PySide.QtGui import *
from PySide.QtCore import *

class BatchProcesser(QThread):
     __errorHappened = False
     def __init__(self, parent=None):
         QThread.__init__(self, parent)
         self.exiting = False
     def run(self):
         for a in range(101):
            print a
            QThread.msleep(100)
            self.emit(SIGNAL("progress(int)"), a)
            print a

以我的理解,这应该在与主线程不同的线程中更新附加到状态栏的进度条。这将允许用户自由地与GUI交互。

现在,如果我尝试冲洗一下,一切正常,直到我点击“处理”菜单。然后,进度条会平缓但不会更新,并且控制台已满,并显示错误-

0 QPixmap:在GUI线程外使用像素图并不安全

QPixmap:在GUI线程外使用像素图并不安全

QPixmap:在GUI线程外使用像素图并不安全

QPixmap:在GUI线程外使用像素图并不安全

0

1个

QPixmap:在GUI线程外使用像素图并不安全

QPixmap:在GUI线程外使用像素图并不安全

QPixmap:在GUI线程外使用像素图并不安全

QPixmap:在GUI线程外使用像素图并不安全

[xcb]出队时队列中的未知请求

[xcb]这很有可能是多线程客户端,尚未调用XInitThreads

[xcb]正在中止,对此感到抱歉。

python:../../src/xcb_io.c:178:dequeue_pending_request:断言`!xcb_xlib_unknown_req_in_deq’失败。

中止(核心已弃用)

我非常需要任何帮助。我无法找出和/或指出错误的原始原因并修复该问题。


问题答案:

问题出在如何将信号连接到插槽上:

      QtCore.QObject.connect(self.processThread, QtCore.SIGNAL("progress(int)"),self.progressBar, QtCore.SLOT("setValue(int)"), QtCore.Qt.DirectConnection)

在这里,您明确地强制执行DirectConnection。错了!它使插槽在调用者的线程中得到处理。这意味着progressBar的GUI更新发生在进程线程中,而不是在GUI线程中。但是,仅在GUI线程中允许绘图。

将信号从一个线程连接到另一个线程的插槽是完全可以的。但是,您需要AutoConnection(默认值,它将识别线程并使用QueuedConnection)或QueuedConnection

此行应解决您的问题:

      QtCore.QObject.connect(self.processThread, QtCore.SIGNAL("progress(int)"),self.progressBar, QtCore.SLOT("setValue(int)"), QtCore.Qt.QueuedConnection)

请参阅http://doc.qt.io/archives/qt-4.7/qt.html#ConnectionType-
enum