PySide and QProgressBar update in a different thre

2019-02-14 21:08发布

问题:

This may be a little long post, so, thanks in advance to be with me till the end. Here is the problem, (i think a fairly basic one, just my inexperience with PiSide and Qt making it harder for me.) I have a main window with one menu item, suppose "Process". the code is following -

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) 
           .

Here self.myappobj refers to a app class I have created which acts as the main logic controller for my app. The code -

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()

So, it is easy to see that what i am trying to do here is to create a main window. Add a menu there called "Process", clicking on which should trigger the callback menuActionProcess method in the app class, where I am making a progress bar and attaching it with the Status Bar of my main window (The actual code has many other things, what i give here is the necessary parts rearranged as an pseudo example) and finally in the BatchProcesser class mentioned in the above code, I am doing this -

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

In my understanding this should update the Progress Bar attached to the Status Bar in a different thread than the main thread. This will allow the user to interact with the GUI freely.

Right now, If i try to rin this, everything is fine until i hit the Process menu. Then, the progress bar appeases but does not update and the console is full with error -

0 QPixmap: It is not safe to use pixmaps outside the GUI thread

QPixmap: It is not safe to use pixmaps outside the GUI thread

QPixmap: It is not safe to use pixmaps outside the GUI thread

QPixmap: It is not safe to use pixmaps outside the GUI thread

0

1

QPixmap: It is not safe to use pixmaps outside the GUI thread

QPixmap: It is not safe to use pixmaps outside the GUI thread

QPixmap: It is not safe to use pixmaps outside the GUI thread

QPixmap: It is not safe to use pixmaps outside the GUI thread

[xcb] Unknown request in queue while dequeuing

[xcb] Most likely this is a multi-threaded client and XInitThreads has not been called

[xcb] Aborting, sorry about that.

python: ../../src/xcb_io.c:178: dequeue_pending_request: Assertion `!xcb_xlib_unknown_req_in_deq' failed.

Aborted (core dumped)

Any help is very much required for me. I am unable to find out and/ot point the original reason of the error and fix the same.

回答1:

The problem lies in how you connect the signal to the slot:

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

Here, you explicitely enforce a DirectConnection. This is wrong! It makes the slot be processed in the caller's thread. This means that the GUI update of the progressBar happens in the process thread, not in the GUI thread. However, drawing is only allowed in the GUI thread.

It is perfectly fine to connect a signal from one thread to a slot from another. However, you need either AutoConnection (the default, which will recognize the threads and use QueuedConnection) or QueuedConnection.

This line should fix your problem:

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

See http://doc.qt.io/archives/qt-4.7/qt.html#ConnectionType-enum.