PySide QThread and QProgressBar
Question: I'm kinda new in GUI programming with PySide, and in Python GUI in general.
I'm trying to set a progress bar value using a thread, but it doesn't work and
there is these errors that keeps on appearing:

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

or

    
    
     QWidget::repaint: Recursive repaint detected 
    

And the program crashes suddenly especially when I'm trying to interact with
another widget in the gui.

* * *

The following is not the actual code, it's only a simulation of what I wanted
to do:

    
    
    from PySide.QtGui import *
    from PySide.QtCore import *​​
    import os, time, platform, sys
    class main(QDialog):
        def __init__(self, parent = None):
            super(main, self).__init__(parent)
            self.resize(300, 100)
            self.setMinimumSize(QSize(300, 100))
            self.setMaximumSize(QSize(300, 100))
            self.setWindowTitle("Test")
            self.buttonStart = QPushButton("Start")
            self.progressBar = QProgressBar()
            self.gridLayout = QGridLayout(self)
            self.setLayout(self.gridLayout)
            self.gridLayout.addWidget(self.progressBar, 0, 0, 1, 1)
            self.gridLayout.addWidget(self.buttonStart, 0, 1, 1, 1)
            self.connect(self.buttonStart, SIGNAL("clicked()"), self.startProgress)
            self.genericThread = GenericThread(self.test)
        def startProgress(self):
            self.genericThread.start()
        def test(self):
            print "started"
            for i in range(100):
                time.sleep(0.3)
                print i
                self.progressBar.setValue(i)
            print "done"
    class GenericThread(QThread):
        def __init__(self, function, *args, **kwargs):
            QThread.__init__(self)
            self.function = function
            self.args = args
            self.kwargs = kwargs
        def run(self):
            self.function(*self.args,**self.kwargs)
            return
    app = QApplication(sys.argv)
    start = main()
    start.show()
    app.exec_()
    

So, the GenericThread is supposed to run any function passed into it in a
thread, instead of creating a thread for each function. I know that I should
be using signals to make a thread change a widget in the gui thread, but
actually I failed to apply that with this thread class. I tried to add the
signal to the test function, and connect it to the main class, but it didn't
do anything.

So what should I do? I don't want to change the thread class GenericThread, as
the actual code has many functions that need to be run in separate threads,
and at the same time I need to show the thread's progress to the user.


Answer: Use a signal to the gui thread to have it update the progress bar and/or draw
the Pixmap.

And when you connect that signal, make sure that you tell it to use a
`Qt::QueuedConnection`, instead of a `Qt::AutoConnection`.

<http://doc.qt.io/qt-4.8/qt.html#ConnectionType-enum>

<http://doc.qt.io/qt-4.8/qobject.html#connect>

<http://doc.qt.io/qt-4.8/qcoreapplication.html#processEvents>

Hope that helps.

