0

I am missing here something. Albeit running in two seperate threads, the UI is still not updated as expected. It is still lagging on the dd worker thread.

from ui import Ui_main_window
from PyQt4 import QtGui, QtCore

import sys
import subprocess
import commands
import threading

from time import sleep

out_int = 0

def _dd_thread_run(_if, _of, _bs, _size):
    _dd_subprocess_command_format = "dd if=%s bs=%s | pv -n --size %s | dd of=%s" % (_if, _bs, _size, _of)
    _dd_subprocess_command = [_dd_subprocess_command_format]
    _dd_progress = subprocess.Popen(_dd_subprocess_command, shell=True, stderr=subprocess.PIPE)
    while _dd_progress.poll() is None:
        out = _dd_progress.stderr.readline().replace("\n", "")
        global out_int 
        out_int = int (out)

def _ui_progress_set():
    class MainWindow(QtGui.QMainWindow, Ui_main_window):
        def __init__(self):
            super(MainWindow, self).__init__()
            self.setupUi(self)

    app = QtGui.QApplication(sys.argv)
    ui = MainWindow()
    ui.show()
    while True:
        for i in range(100):
            ui.progressBar.setValue(out_int)
            sleep(.1)


t1 = threading.Thread(target=_dd_thread_run, args = ["/dev/urandom", "/dev/null", "100K", "100M"])
t1.start()
t2 = threading.Thread(target=_ui_progress_set, args = [])
t2.start()

I suspect a Python or PyQt bug? And it stays the same, no matter where the UI class is defined.

2
  • Possible duplicate of Label in PyQt4 GUI not updating with every loop of FOR loop Commented Dec 9, 2015 at 0:05
  • Note that Qt is only supposed to be accessed from the main thread, which likely also explains the crashes you have seen in some of your other questions. You need to re-architect your program so that Qt in in the main thread, and use QThreads to emit signals back to the main thread to update the GUI. Commented Dec 9, 2015 at 0:08

1 Answer 1

0

You don't have the main eventloop running anywhere - you won't get a gui to show up without that (see doc). Try

app = QtGui.QApplication(sys.argv)
ui = MainWindow()
ui.show()
app.exec_()

without the while loop. If your while loop is supposed to create an indefinite progressbar - that can easily be achieved by setting the QProgressBar minimum and maximum to zero.

Sign up to request clarification or add additional context in comments.

3 Comments

I do. I have it in the second thread, the _ui_progress_set(): The program is working fine, only the UI is updating slow, as if the threads were not separated. That is the problem. But the only thing connecting the two threads is a progress variable, called out_int, which is an integer from 0 to 100.
I can't find any exec_ invocation in your code!? Plus, it if it was there, exec_ blocks further execution until the application ends - so you wouldn't actually reach the while loop.
I missed it sorry. But then i have a new problem, in that i can not update the status bar as the code is halted. But if i separate the status bar update into another function, it then can not reach the "ui" element, which is declared in the first function. How should i proceed?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.