1

I'm having a go at my first attempted at creating a GUID with Pyside and QT.

I looked at some tutorials and created this attempted below.

I'm trying to create some sort of a window that prints what ever the script finds, so instead of using the python shell window, it prints at this new window I created. The App runs and creates the GUID, the bottom lunches the script but now is where I'm stuck.

The app freezes until the script has run to the end instead of printing the results as it goes though the loop.

here is the code:

import sys
import PySide
from PySide.QtCore import *
from PySide.QtGui import *
import time

class Form(QDialog):
    def __init__(self,parent=None):
        super(Form, self).__init__(parent)

        self.browser = QTextBrowser()
        lable = QLabel("Print Window:")
        self.botton = QPushButton('Start Search')

        self.botton.setMaximumWidth(150)

        layout = QVBoxLayout()
        layout.addWidget(lable)
        layout.addWidget(self.browser)
        layout.addWidget(self.botton)
        self.setLayout(layout)
        self.setWindowTitle("Test App")
        self.setMinimumWidth(650)

        self.connect(self.botton, SIGNAL('clicked()'), self.run)


    def run(self):
        TestList = ['loop 1','loop 2','loop 3','loop 4','loop 5']
        for i in TestList:
            self.browser.append(i)
            time.sleep(1)


app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()

I have been learning python by myself for a couple of weeks, and thanks mainly to this website I already managed to learn a lot and create some apps.

hope someone can help me out with this one, I can't seem to figure it out. Thanks so much

kiko va
  • 55
  • 2
  • 7

1 Answers1

0

The reason is because you have a blocking loop in here:

def run(self):
    TestList = ['loop 1','loop 2','loop 3','loop 4','loop 5']
    for i in TestList:
        self.browser.append(i)
        time.sleep(1)

This means, the main Qt event loop has no chance to process your append request through the regular events. The usual solution for this issue is using the following method in Qt:

void QCoreApplication::processEvents(QEventLoop::ProcessEventsFlags flags = QEventLoop::AllEvents) [static]

Processes all pending events for the calling thread according to the specified flags until there are no more events to process.

You can call this function occasionally when your program is busy performing a long operation (e.g. copying a file).

In the event that you are running a local loop which calls this function continuously, without an event loop, the DeferredDelete events will not be processed. This can affect the behaviour of widgets, e.g. QToolTip, that rely on DeferredDelete events to function properly. An alternative would be to call sendPostedEvents() from within that local loop.

Calling this function processes events only for the calling thread

This means, you would be writing something like this:

...
def run(self):
    TestList = ['loop 1','loop 2','loop 3','loop 4','loop 5']
    for i in TestList:
        self.browser.append(i)
        QCoreApplication.processEvents()
        time.sleep(1)
...
Community
  • 1
  • 1
László Papp
  • 51,870
  • 39
  • 111
  • 135