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.

Trevor
  • 1,111
  • 2
  • 18
  • 30
Gala
  • 2,592
  • 3
  • 25
  • 33
  • Possible duplicate of [Label in PyQt4 GUI not updating with every loop of FOR loop](http://stackoverflow.com/questions/2482437/label-in-pyqt4-gui-not-updating-with-every-loop-of-for-loop) – three_pineapples Dec 09 '15 at 00: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 `QThread`s to emit signals back to the main thread to update the GUI. – three_pineapples Dec 09 '15 at 00:08

1 Answers1

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.

sebastian
  • 9,526
  • 26
  • 54
  • 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. – Gala Dec 07 '15 at 19:44
  • 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. – sebastian Dec 07 '15 at 19:51
  • 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? – Gala Dec 07 '15 at 20:35