Peter Wang
Peter Wang

Reputation: 1838

PyQt4: Only the last signal is being processed

I ran into a strange problem when working on my project. I have a GUI and a QTextEdit that serves as a status browser. When a button is clicked, I want the QTextEdit to display a 10 second countdown while another function is happening in a separate thread. Even though I emit a signal every second, the QTextEdit hangs for 9 seconds, then displays the last countdown number.

I thought this might have something to do with stuff happening in a separate thread, so I created a separate example to test this out. In my simple example, there are two things: a QTextEdit and a Button. When the button is clicked, the status browser should display '5' for two seconds, then '4'.

Here is the code:

import sys
from PyQt4 import QtGui, uic
from PyQt4.QtCore import QObject, pyqtSignal
import time

class MainUI(QObject):
    status_signal = pyqtSignal(str)

    def __init__(self, window):
        super(QObject, self).__init__()
        self.ui = uic.loadUi(r'L:\internal\684.07\Mass_Project\Software\PythonProjects\measure\testing\status_test.ui', window)

        self.ui.statusBrowser.setReadOnly(True)
        self.ui.statusBrowser.setFontPointSize(20)

        self.status_signal.connect(self.status_slot)
        self.ui.button.clicked.connect(self.counter)

        window.show()

    def status_slot(self, message):
        self.ui.statusBrowser.clear()
        self.ui.statusBrowser.append(message)

    def counter(self):
        print 'clicked'
        i = 5
        self.status_signal.emit(str(i))
        time.sleep(2)
        self.status_signal.emit(str(i-1))


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    app.setStyle("cleanlooks")
    main_window = QtGui.QDialog()
    main_ui = MainUI(main_window)
    sys.exit(app.exec_())

In this example, the same thing happens. The status browser hangs for 2 seconds, then only displays '4'. When I alter the status_slot function so that it doesn't clear the status browser before appending to it, the status browser waits for 2 seconds, then emits both signals at once, displaying '5 \n 4'. Does anyone know why this is happening and what I can do to constantly update the display? Thanks in advance!

Upvotes: 0

Views: 90

Answers (1)

three_pineapples
three_pineapples

Reputation: 11869

time.sleep() blocks the Qt main loop so it can't process window redraw events. Use a QTimer to periodically call a method which emits your signal so that control is returned to the Qt event loop regularly.

Upvotes: 1

Related Questions