[PySide] Updating progress bar value through a thread

Dina Taha deetaha.deetaha at gmail.com
Mon Jun 24 04:59:21 CEST 2013


Hi all,

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.


-- 

Yours;
     Dina A.A. Taha
*> Software engineering program <*
*
> Faculty of Computer and Information - Helwan University <
*
________________________________________________________________
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20130624/abcefa69/attachment.html>


More information about the PySide mailing list