0


I'm having problem with actively updating my TextEdit box from PyQt. I want to make an app that will download files in parts (new thread for each part, downloading parallely) and update the current status of each part in textbox, but my app "freezes" for the downloading time and sets the textbox after downloading is complete although if I print the result it looks fine, no freeze on console.

I know that this code is "a mess" right now, but I was changing many things and experimented with different approaches.

I marked this "print" which works fine, and just below there is setText which freezes my app for the downloading time.

If it's the problem with "TextEdit" from PyQt please let me know, I'll change it but I didn't find any information like that so far.
Thanks!

    def supervi(self):


    import os
    import urllib2
    N=2
    url = self.__url
    dir = self.path
    f_name = url.split("/")[len(url.split("/")) - 1]
    dir_tmp=dir + "\\TMP." + f_name
    if os.path.isdir(dir_tmp) == False:  
        os.mkdir(dir_tmp)
    for n in range(0,N):
        with open(dir_tmp+"\\file"+str(n), "w+b") as f:
            #f.write("")
            pass

    data = urllib2.urlopen(url)

    file_size = int(data.headers["Content-Length"].strip())

    import multiprocessing as mp
    data_block = file_size/N
    p=mp.Pool(N)
    for i in range(0, N):
        start = i * data_block
        stop = 0
        if not i == N - 1:
            stop = i * data_block + data_block - 1
        else:
            stop = file_size

        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0",
            "Accept-Encoding": "gzip, deflate, sdch",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
            "Accept-Language": "pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4",
            "Connection": "keep-alive",
            "Range": "bytes=" + str(start) + "-" + str(stop)
        }

        req = urllib2.Request(url, headers=headers)
        from main import dziecko
        p.apply_async(dziecko,[i,req,dir_tmp])

    while True:
        sum=0
        for n in range(0,N):
            sum=sum+os.path.getsize(dir_tmp + "\\file" + str(n))

        if not sum < file_size:
            from main import del_and_combine
            del_and_combine(dir,dir_tmp,f_name,N)
            break
        for n in range(0,N):
            size=os.path.getsize(dir_tmp + "\\file" + str(n))
            print size           ##################THIS ONE
            self.url.setText(str(os.path.getsize(dir_tmp + "\\file0")))
Pawel
  • 105
  • 1
  • 6

1 Answers1

1

Add : QtCore.QCoreApplication.processEvents() inside your loop. This will update the text every iteration. Without this, PyQt will always freeze during loops.

For more information : < pyqt-gui-freezes-while-in-loop >

Community
  • 1
  • 1
Olivier Giniaux
  • 870
  • 2
  • 8
  • 22
  • While this may work, it would be far better to not block the Qt event loop in the first place by using a `QThread` (see the second answer to the question linked to in this answer) – three_pineapples Jun 28 '15 at 04:26
  • Thank you, processEvents() works fine.I'll maybe try to use QThread later, but now it should do the work – Pawel Jun 28 '15 at 08:30