I've encountered this problem twice now in subtle ways, I think this is at the root of my last question about the QWebView; I've included sample code this time to illustrate.

In the attached script, I have a widget class and a processor class. The processor represents some kind of processing engine that's going to do a lengthy multi-part routine of some kind. The widget class is the gui for the engine.

The "go" button is connected to the "process" method of the processor object. The process() method emits a signal with a string, does process A for 10 seconds, emits a signal with a string, does process B for 10 seconds, and emits a final signal with a string.

The widget class is supposed to be picking up the signals and displaying the strings. So the expected output is:

<click "go">
first message displays
<wait 10 seconds>
second message displays
<wait 10 seconds>
final message displays

The actual output is that nothing happens for 20 seconds, then all the text displays. Because the widget isn't going to process any received signals until the called method exits.

How do I produce the expected behavior here?
from PyQt4.QtGui import *
from PyQt4.QtCore import *

import sys
from time import sleep

class mywidget(QWidget):
    def __init__(self):
        super(QWidget, self).__init__()
        self.gobutton = QPushButton("Go")
        self.logwindow = QTextEdit()
        self.setLayout(QVBoxLayout())
        self.layout().addWidget(self.gobutton)
        self.layout().addWidget(self.logwindow)

        self.processor = processor()
        self.connect(self.processor, SIGNAL("statusChanged(str)"), self.logwindow.append)
        self.connect(self.gobutton, SIGNAL("clicked()"), self.processor.process)

         
class processor(QObject):
    def __init__(self):
        super(QObject, self).__init__()

    def process(self):
        self.emit (SIGNAL("statusChanged(str)"), "Starting A\n")
        #simulating some lengthy process A
        sleep(10)
        self.emit (SIGNAL("statusChanged(str)"), "A done, starting B\n")
        #simulating some lengthy process B
        sleep(10)
        self.emit (SIGNAL("statusChanged(str)"), "B done. Finished\n")


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = mywidget()
    w.show()
    app.exec_()
_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Reply via email to