Search code examples
pythonuser-interfacepyqtpysideqprogressbar

How to ensure QProgressDialog is shown in PyQt


Sometimes my QProgressDialog shows, sometimes it doesn't ever show at all (as if processEvents weren't called). Are there any artifacts of the processEvents() command that may cause the QProgressDialog not to show under certain circumstances?

My question is general because I have not yet been able to isolate the problem in my code. However, I have noticed that when my QProgressDialog does not show it occurs when I am accessing a text file using a config parser. The work around was to do a time.sleep() after the file has been closed (perhaps to ensure the process completed and that processEvents would then commence showing the QProgressDialog).

If it helps, here's my code for running the QProgressDialog as a generator:

def progress_dialog(self, data, label, window_title, stop_label, capture_bar=False):
    bar = QProgressDialog(label, stop_label, 0, len(data))
    if capture_bar: self.prog_bar = bar
    bar.setWindowTitle(window_title)
    for k, d in enumerate(data):
        QCoreApplication.instance().processEvents()
        if bar.wasCanceled():
            raise GeneratorExit
        # set the next value beyond the start of 0
        bar.setValue(k+1)
        # again process events to draw the new label and value
        QCoreApplication.instance().processEvents()
        yield(d)
        raise StopIteration

Again, sorry I don't have a full code snippet of the isolated problem (and the full code is too big of an ocean). I guess what I'm looking for is a why of checking if the processEvents() command is doing its job (because clearly I am calling it but it hangs on other processes rather than showing the dialog).

Edit:

According to this support request doing a "bar.show()" command will force the progress bar to show.

http://redmine.smar.fi/issues/265

I'm going to wait a few weeks and make sure this is a guaranteed fix before posting it as an answer.


Solution

  • According to this support request doing a bar.show() command will force the progress bar to show: http://redmine.smar.fi/issues/265

    Simply call the show() method before every process events call and after the progress bar is first constructed.

    I've waited nearly 4 months and this solution has worked without failing yet. Seems to be a sufficient answer.