各种进度条的使用

说明:代码来自开源中国,所有权归原作者所有

 

代码:

  1 #!/usr/bin/env python
  2 # --*--codig: utf8 --*--
  3 
  4 from PyQt4 import QtGui
  5 from PyQt4 import QtCore
  6 
  7 class BaseProgressDialog(QtGui.QWidget):
  8     updateProgress = QtCore.pyqtSignal(str)
  9     def __init__(self, text='', parent=None):
 10         super(BaseProgressDialog, self).__init__(parent)
 11         self.setFixedHeight(50)
 12         self.text  = text
 13         self.progressbar = QtGui.QProgressBar( )
 14         self.progressbar.setTextVisible(True)
 15         self.updateProgress.connect(self.setValue)
 16 
 17         self.bottomBorder = QtGui.QWidget( )
 18         self.bottomBorder.setStyleSheet("""
 19             background: palette(shadow);
 20         """)
 21         self.bottomBorder.setSizePolicy(QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Fixed))
 22         self.bottomBorder.setMinimumHeight(1)
 23 
 24         self.label  = QtGui.QLabel(self.text)
 25         self.label.setStyleSheet("""
 26             font-weight: bold;
 27         """)
 28         self.layout = QtGui.QVBoxLayout( )
 29         self.layout.setContentsMargins(10,0,10,0)
 30         self.layout.addWidget(self.label)
 31         self.layout.addWidget(self.progressbar)
 32 
 33         self.mainLayout = QtGui.QVBoxLayout( )
 34         self.mainLayout.setContentsMargins(0,0,0,0)
 35         self.mainLayout.addLayout(self.layout)
 36         self.mainLayout.addWidget(self.bottomBorder)
 37         self.setLayout(self.mainLayout)
 38         self.totalValue = 0
 39 
 40     def setValue(self, value):
 41         self.totalValue += len(value)
 42         self.progressbar.setValue(self.totalValue)
 43 
 44     def setMax(self, value):
 45         self.progressbar.setMaximum(value)
 46 
 47 class DownloadProgressBar(BaseProgressDialog):
 48     def __init__(self, text='Downloading', parent=None):
 49         super(self.__class__, self).__init__(text, parent)
 50         style ="""
 51         QProgressBar {
 52             border: 2px solid grey;
 53             border-radius: 5px;
 54             text-align: center;
 55         }
 56 
 57         QProgressBar::chunk {
 58             background-color: #37DA7E;
 59             width: 20px;
 60         }"""
 61         self.progressbar.setStyleSheet(style)
 62 
 63 
 64 class UploadProgressBar(BaseProgressDialog):
 65     def __init__(self, text='Uploading', parent=None):
 66         super(self.__class__, self).__init__(text, parent)
 67         style ="""
 68         QProgressBar {
 69             border: 2px solid grey;
 70             border-radius: 5px;
 71             text-align: center;
 72         }
 73 
 74         QProgressBar::chunk {
 75             background-color: #88B0EB;
 76             width: 20px;
 77         }"""
 78         self.progressbar.setStyleSheet(style)
 79 
 80 class ProgressDialog(QtGui.QMainWindow):
 81     def __init__(self, parent=None):
 82         super(self.__class__, self).__init__(parent)
 83         self.resize(500, 250)
 84         self.scrollArea = QtGui.QScrollArea( )
 85         self.scrollArea.setWidgetResizable(True)
 86         self.setCentralWidget(self.scrollArea)
 87 
 88         self.centralWidget = QtGui.QWidget( )
 89         self.scrollArea.setWidget(self.centralWidget)
 90 
 91         self.layout = QtGui.QVBoxLayout( )
 92         self.layout.setAlignment(QtCore.Qt.AlignTop)
 93         self.layout.setContentsMargins(0,10,0,0)
 94         self.centralWidget.setLayout(self.layout)
 95 
 96     def addProgressbar(self, progressbar):
 97         self.layout.addWidget(progressbar)
 98 
 99 if __name__ == "__main__":
100     import random
101 
102     app = QtGui.QApplication([])
103     progressNumbers = [x for x in range(1, 101)]
104     progressItems = []
105 
106     while len(progressItems) <= 20:
107         progressItems.append(random.choice(progressNumbers))
108 
109     progressDialog = ProgressDialog()
110 
111     for progressItem in progressItems:
112         progressBar = DownloadProgressBar(text="download")
113         progressBar.setMax(100)
114         progressBar.setValue(' ' * progressItem)
115         progressDialog.addProgressbar(progressBar)
116 
117     for progressItem in progressItems:
118         progressBar = UploadProgressBar(text="Upload")
119         progressBar.setMax(100)
120         progressBar.setValue(' ' * progressItem)
121         progressDialog.addProgressbar(progressBar)
122 
123     progressDialog.show()
124     app.exec_()

 

posted @ 2015-05-26 12:47  Chris*Chen  阅读(701)  评论(0编辑  收藏  举报