[PyQt] Make a exec_() in a Dialog

rudsonalves at yahoo.com.br rudsonalves at yahoo.com.br
Sun Nov 16 12:39:29 GMT 2008


Hi people,

Only to register:

I did the dialog for download programs with use of qApp.processEvents(),
to upgrade events, and repaint() to redraw the dialog. The upgrade is
controled by variable SPEED_TIME, in seconds (0.3s).

The Ui_DownloadDialog.py has a QProgressBar named downloadProgressBar,
three QLabel (urlLabel, destinyLabel and rateLabel), and one pushButton
(closeButton).

I don't know if it is the best form to do, but is the form I found. If
anybody have another suggestion, pease, send it.

See the code:
-----------------------------------------
class DownloadDlg(QDialog, Ui_DownloadDialog):
    """ Download program: Thanks to Andrew Pennebaker
(andrew.pennebaker at gmail.com)". See:
http://snippets.dzone.com/posts/show/2887
    proxy is a dictionary with form:
    {'http': 'http://proxy.ip.address:port', 'ftp' : '...', 'gopher' :
'...'} """
    url = ''
    destiny = ''
    proxy = None
    backup = False
    __close = False

    def __init__(self, url, destiny, proxy = None, parent = None):
        super(DownloadDlg, self).__init__(parent)
        self.setupUi(self)

        self.url = url
        self.destiny = destiny
        self.proxy = proxy

        self.urlLabel.setText('')
        self.destinyLabel.setText('')
        self.rateLabel.setText('Wait! Make connection to\n%s' % self.url)
        self.downloadProgressBar.setValue(0)

        QTimer.singleShot(100, self.start)

    @pyqtSignature('')
    def on_closeButton_clicked(self):
        self.__close = True

    def start(self):
        """ Start download... """
        if self.url != '':
            self.repaint()
            qApp.processEvents()
            status = self.wget_file()
        else:
            QMessageBox.warning(self, 'Download error', 'Invalid url')
            status = False

        if status:
            QDialog.accept(self)
        else:
            QDialog.reject(self)

    def createDownload(self):
        """ createDownload(url, proxy = None): create a new download
link """
        # open url link
        instream = urlopen(self.url, None, self.proxy)
        # read legth of file
        length = instream.info().getheader('Content-Length')
        # check legth read
        if length == None:
            length = '?'

        return (instream, length)

    def wget_file(self):
        """ wget_file(): make download of url and save it in destiny. """

        # Constants
        SPEED_TIME = .3

        # output status
        status = True

        # create new output file
        outfile = open(self.destiny, 'wb')
        # get file name
        fileName = outfile.name.split('/')[-1]

        # create a new download link and read legth of file
        url, length = self.createDownload()

        # check legth load
        if length != '?':
            length = float(length)
        else:
            length = 0
        # start bytesread and speed
        bytes_read = .0                   # all bytes read
        speed = 0                         # medium speed

        # initial_time
        index = 1                         # index of loop
        start_time = time()               # start time

        self.urlLabel.setText('From: %s' % self.url)
        self.destinyLabel.setText('To: %s' % self.destiny)

        # read lines from url
        for line in url:
            # increase bytesread
            bytes_read += len(line)

            # read t_final
            delta_time = time() - start_time

            # Calcule new speed if delta_time >= SPEED_TIME
            if delta_time >= index*SPEED_TIME:
                index += 1
                speed = bytes_read/delta_time

                if length != 0:
                    self.rateLabel.setText('Rate: %s/s   Read: %s/ %s' %
(human_read(speed), human_read(bytes_read), human_read(length)))
                else:
                    self.rateLabel.setText('Rate: %s/s   Read: %s' %
(human_read(speed), human_read(bytes_read)))

               
self.downloadProgressBar.setValue(int(100*bytes_read/length))
                self.repaint()

                # Process new events
                qApp.processEvents()
                if self.__close:
                    status = False
                    break

            # write line in output file
            outfile.write(line)

        # close url and output file
        url.close()
        outfile.close()

        return status


-----------------------------------------
Thanks for all
Rudson Alves
> Hi David,
>
> I need insert operations, as get file parts from internet, in main loop
> of the QDialog.
>
> I try to use the QTimer.singleShot to start the wget_file, but it freeze
> all dialog buttons (as Cancel Button) until the wget_file routine return.
>
> know who I can make it?
>
>> On Sat Nov 15 17:24:08 GMT 2008, rudsonalves wrote:
>>
>>   
>>> I am implementing a dialogue to download programs in an application.  I
>>> need to start the dialog to download a file, only after the call of
>>> .exec_().
>>>
>>> I try implement a .exec_() method, but it no open dialog window. See code:
>>>     
>> [...]
>>
>>   
>>>     def exec_(self):
>>>         print 'Go, go, go ...'
>>>         self.show()
>>>         if self.urlLabel.text() != '':
>>>             print 'Start...'
>>>             self.wget_file() # Download routine
>>>         else:
>>>             QMessageBox.Warning(self, 'Invalid url')
>>>             QDialog.accept(self)
>>>     
>> Normally, you only need to call the exec_() method, but if you want to
>> reimplement it to add extra features, you should call the base class's
>> implementation instead of calling its show() method:
>>
>>     def exec_(self):
>>         print 'Go, go, go ...'
>>         QDialog.exec_(self)
>>         # ...
>>
>> David
>> _______________________________________________
>> PyQt mailing list    PyQt at riverbankcomputing.com
>> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
>>
>>   
>
> __________________________________________________
> Faça ligações para outros computadores com o novo Yahoo! Messenger 
> http://br.beta.messenger.yahoo.com/ 
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
>


		
_______________________________________________________ 
Yahoo! Mail - Sempre a melhor opção para você! 
Experimente já e veja as novidades. 
http://br.yahoo.com/mailbeta/tudonovo/
 



More information about the PyQt mailing list