Hi to all and great for me to be here!
I was previously used to C# and delegates to communicate from Main Form's thread with a worker thread that i spawn from that form and now i use Python and Qt and trying to do the same thing.
I have the very simple example of counting from "start" to "end" and just display the progress in a progress bar.
The routine that does that is running on a secondary thread. I have made the Gui with Qt Designer and i have the following code that i wrote:
import sys
import threading
from PyQt4 import QtCore, QtGui
from calculatorform_ui import Ui_CalculatorForm
from ui_countNumbers import Ui_MainWindow
from time import sleep
def __init__(self, parent=None):
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
@QtCore.pyqtSignature("")
def on_start_clicked(self):
t = threading.Thread(self.doCount())
t.start()
def doCount(self):
start = self.ui.spFrom.value()
end = self.ui.spTo.value() + 1
for x in range(start, end):
self.ui.progres.setValue(x)
sleep(1)
app.processEvents()
if __name__ == "__main__":
count = CountNumbersForm();
count.show()
sys.exit(app.exec_())
import sys
import threading
from PyQt4 import QtCore, QtGui
from calculatorform_ui import Ui_CalculatorForm
from ui_countNumbers import Ui_MainWindow
from time import sleep
class CountNumbersForm(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
@QtCore.pyqtSignature("")
def on_start_clicked(self):
t = threading.Thread(self.doCount())
t.start()
def doCount(self):
start = self.ui.spFrom.value()
end = self.ui.spTo.value() + 1
for x in range(start, end):
self.ui.progres.setValue(x)
sleep(1)
app.processEvents()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
count = CountNumbersForm();
count.show()
sys.exit(app.exec_())
To copy to clipboard, switch view to plain text mode
(spFrom and spTo are spin boxes, "start" is a button that to press to do the job)
Apart from the fact that progress bar does not updated correctly (it stops updating at some percent and not at 100%), my main problem is that main thread (ie. the main Form) is not so responsive as i thought it would be.
I read somewhere that Qt's Signals and Slots mechanism is working correctly in the cross-thread situation but apparently i have made something wrong and thus program hase that behavior.
Can anyone please help me with this ? How can i reliably communicate from Form's main thread to it's working thread in Qt ?
Thanks a lot for any help!
Bookmarks