Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Reply
 
LinkBack Thread Tools Display Modes
Old 08-05-2011, 01:17 PM   #1 (permalink)
Alpha Geek
 
Join Date: Jan 2007
Location: In your hearts
Posts: 828
Default problem in PyQt4 program


Hello everyone, I have started creating a Download Manager for Linux using Python with PyQt4 as GUI toolkit and PycURL as library for downloading files. My problem is that whenever I start a download GUI stops. It works in the background and just keeps on downloading but no response from GUI.
Code is Here.

main.py
Code:
    from PyQt4 import QtCore,QtGui
    import downloadclass,winaddnew,treeitem
    import sys

    class Downloader(QtGui.QMainWindow):

        def __init__(self,parent=None):

            QtGui.QMainWindow.__init__(self,parent)

            self.setWindowTitle("Download")
            self.setGeometry(10,10,600,400)
         
            self.tablewidget = QtGui.QTableWidget(self)
            self.setCentralWidget(self.tablewidget)
            self.tablewidget.setColumnCount(6)
            self.tablewidget.setHorizontalHeaderLabels(['File Name','Url','Size','Downloaded','Time Remaining','Speed','Status'])
            self.tablewidget.setRowCount(1)
            self.tablewidgetrowitemarray = []
                                                       
            self.tablewidget.horizontalHeader().setClickable(False)
            self.tablewidget.cellClicked.connect(self.cellclicked)

            menubar = self.menuBar()
            newdownload = QtGui.QAction("Create a new download",self)
            start = QtGui.QAction("Start",self)
            stop = QtGui.QAction("Stop",self)
            pauseresume = QtGui.QAction("Pause/Resume",self)
            startall = QtGui.QAction("Start All",self)
            stopall = QtGui.QAction("Stop All",self)
            pauseresumeall = QtGui.QAction("Pause/Resume All",self)
            restart = QtGui.QAction("Restart Download",self)
           
            downloadmenu = menubar.addMenu("Download")
            downloadmenu.addAction(newdownload)
            downloadmenu.addAction(start)
            downloadmenu.addAction(stop)
            downloadmenu.addAction(pauseresume)
            downloadmenu.addAction(startall)
            downloadmenu.addAction(stopall)
            downloadmenu.addAction(pauseresumeall)
            downloadmenu.addAction(restart)

            self.addwin = winaddnew.addnewwin()
           
            self.url = ''
            self.path = ''
            self.filename = ''
            self.rowselected = 0
           
            self.downloaditem = treeitem.downloaditem
            self.dwlclass = downloadclass.downloadfile
            self.downloaditemarray = []
           
            self.connect(newdownload,QtCore.SIGNAL('triggered()'),self.funcshowaddwin)
            self.connect(start,QtCore.SIGNAL('triggered()'),self.funcstart)
               
            self.Timer = QtCore.QTimer(self)
            self.Timer.setInterval(200)
            self.connect(self.Timer, QtCore.SIGNAL('timeout()'),self.functimer)
           
        def cellclicked(self,row,col):

            self.tablewidget.selectRow(row)
            self.rowselected = row

        def funcshowaddwin(self):

            self.Timer.start()
            self.addwin.show()
       
        def functimer(self):

            self.url,self.path = self.addwin.funcpass()
            if self.url != '':
                if self.path !='':
                    self.Timer.stop()
                    self.adddownload()

        def adddownload(self):

            self.downloaditemarray.append(self.downloaditem(self.getfilename(self.url),self.url,0,'Downloading'))
            self.createrow(self.downloaditemarray[len(self.downloaditemarray)-1])

        def createrow(self,downloaditem):

            for i in range(0,6):
                self.tablewidgetrowitemarray.append(QtGui.QTableWidgetItem(0))
                self.tablewidgetrowitemarray[i].setFlags(QtCore.Qt.ItemIsSelectable)
                self.tablewidget.setItem(0,i,self.tablewidgetrowitemarray[i])

            self.tablewidgetrowitemarray[0].setText(downloaditem.filename)
            self.tablewidgetrowitemarray[1].setText(downloaditem.url)
            self.tablewidgetrowitemarray[2].setText(str(downloaditem.size))
            self.tablewidgetrowitemarray[3].setText("")
            self.tablewidgetrowitemarray[4].setText("")
            self.tablewidgetrowitemarray[5].setText("")
            #self.tablewidgetrowitemarray[6].setText(downloaditem.status)
           
        def getfilename(self,url):

            lst = url.split('/')
            return lst[len(lst)-1]

        def funcstart(self):

            d = self.dwlclass(self.path,str(self.tablewidget.item(self.rowselected,1).text()),self.tablewidget.item(self.rowselected,2).text())
            self.tablewidgetrowitemarray[2].setText(str(d.size))
            d.start()
           
           
    app = QtGui.QApplication(sys.argv)
    d = Downloader()
    d.show()
    app.exec_()
downloadclass.py

Code:
    import pycurl,os

    class downloadfile(object):

        def __init__(self,path,url,downloaded):

            self.url = url
            self.path = path
            self.downloaded = downloaded
            self.size = 0
            self.bytesdownloaded = 0
            self.initial = 0
            self.final = 0
           
            self.curl = pycurl.Curl()
            self.curl.setopt(pycurl.URL,self.url)
                   
        def progress(self,download_t,download_d,upload_t,upload_d):

            self.initial = download_d
            self.size = download_t
            self.bytesdownloaded = download_d
            self.speed = self.final - self.initial
            self.final = download_d
           
        def start(self):
           
            f = open(self.path,"wb")
            self.curl.setopt(pycurl.WRITEDATA,f)
            self.curl.setopt(pycurl.NOPROGRESS,0)
            self.curl.setopt(pycurl.PROGRESSFUNCTION,self.progress)
            self.curl.perform()

        def resume(self):

            f = open(self.path,"ab")
            self.curl.setopt(pycurl.RESUME_FROM,os.path.getsize(self.path))
            self.curl.setopt(self.pycurl.WRITEDATA,f)
            self.curl.setopt(pycurl.NOPROGRESS,0)
            self.curl.setopt(pycurl.PROGRESSFUNCTION,self.progress)
            self.curl.perform()
Can anyone solve my problem or atleast give me a hint??
abhijangda is online now   Reply With Quote
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 09-05-2011, 12:47 AM   #2 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: problem in PyQt4 program

You're downloading within the application's thread. That would block it, naturally.

Try threading out your downloader operations as a solution, making it report some progress only when required. It would be a good exercise to add to your skill-building.
__________________
Harsh J
www.harshj.com
QwertyManiac is offline   Reply With Quote
Old 09-05-2011, 12:51 AM   #3 (permalink)
Alpha Geek
 
Join Date: Jan 2007
Location: In your hearts
Posts: 828
Default Re: problem in PyQt4 program

Thanks for reply, are you talking about threads in python??
abhijangda is online now   Reply With Quote
Old 09-05-2011, 01:19 AM   #4 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: problem in PyQt4 program

Yes, you should be able to use Python's own threads over QThreads. However, the latter supports the whole signals-and-slots concept, if you swing that way.
__________________
Harsh J
www.harshj.com
QwertyManiac is offline   Reply With Quote
Old 09-05-2011, 09:04 AM   #5 (permalink)
gkbhat.blogspot.com
 
Join Date: Apr 2008
Location: Mangalore/Bangalore
Posts: 103
Default Re: problem in PyQt4 program

You should use threading. I do not know about Python threads, but QThread can do the job very well for you.
__________________
blogging at http://gkbhat.blogspot.com
gk2k is offline   Reply With Quote
Old 09-05-2011, 10:42 AM   #6 (permalink)
Alpha Geek
 
Join Date: Jan 2007
Location: In your hearts
Posts: 828
Default Re: problem in PyQt4 program

thanks for reply!!
abhijangda is online now   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by Charan
- by Sarath
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 12:27 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2