-1

I have a list of dictionaries like dict = {'text': 'abcd', 'delay': 1.234}. I have a loop which for each dictionary of the list wait for the 'delay' and after do something with the 'text':

for element in myList:
    time.sleep(element['delay'])

    self.ui.textEdit.append(element['text'])
    print element['text']
    myFunction(element['text'])

The sleep, print and myFunction work as they should work. But the textEdit never change and in title of my window I have: 'Not responding'. How can I avoid this problem ?

Alain
  • 380
  • 1
  • 5
  • 15

2 Answers2

3

Your code is blocking Qt's main event loop. The GUI update events are queued and won't be processed until the event loop is allowed to run again.

A possible workaround would be to call QtCore.QCoreApplication.processEvents() after you append the text to textEdit to force the event loop to run and process events in the queue.

A more general solution would be to move your code into a worker thread so that the main thread is free to continue processing GUI events. Take a look at the answers by myself and Shadow9043 in this question for further information. The question itself is somewhat different to yours but the solution is similar (note that coded example is written in PyQt so may be a little different to PySide).

Community
  • 1
  • 1
user3419537
  • 4,740
  • 2
  • 24
  • 42
1

wanted to add comment but don't have 50 reputation so here we go :pyside and sleep issue question has already been asked and answered here PySide / wait or sleep :(

Community
  • 1
  • 1
hemraj
  • 964
  • 6
  • 14
  • I already saw this question and this answer. But the problem is my list can have lots of elements (more than 100 000), and Pyside don't really like to create 100 000 `QTimer`... – Alain Jul 02 '14 at 10:25
  • @user3771487 Do you really need 100000 indepedent timers? Can't you just run one timer, then execute the action and run the next timer? – NoDataDumpNoContribution Jul 03 '14 at 08:25
  • What do you mean by "run the next timer" ? – Alain Jul 03 '14 at 08:34