1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| #!/usr/bin/python
# -*- coding: utf-8 -*-
# Inclusions
# Native
import sys
# Pyqt4
from PyQt4.QtCore import *
from PyQt4.QtGui import *
#############################################################################
class mainWidget(QWidget):
#========================================================================
def __init__(self, parent=None):
super(mainWidget,self).__init__(parent)
self.setGeometry(150, 150, 880, 200)
self.setMinimumSize(880, 200)
self.setStyleSheet(
"QProgressBar {color:black; text-align:center; font-weight:bold; padding:2px;}"
"QProgressBar:chunk {background-color: #0088dd; width: 10px; margin-left:1px;}"
)
########################
# Var Initialisation #
########################
self.parent = parent
self.fileList = [
["Valeur 1", "Commentaire 1", "", 33],
["Valeur 2", "Commentaire 2", "", 66],
["Valeur 3", "Commentaire 3", "", 100]
]
self.tmheader = ['Lien', 'Nom', 'Progression']
#########################
# Window Construction #
#########################
#ListView
self.listView = QTableView()
#Progress Barre
self.barre = QProgressBar(self)
self.barre.reset()
self.barre.setRange(0, 0)
self.barre.setValue(0)
#####################
# Window Position #
#####################
posit = QGridLayout()
posit.addWidget(self.listView, 1, 0, 1, 4)
posit.addWidget(self.barre, 7, 0, 1, 4)
self.setLayout(posit)
#################################
# Check Option/Update/Session #
#################################
self.refreshTable()
# Table Model
#========================================================================
def MyTableModel(self):
pm = QStandardItemModel()
count = 0
for header in self.tmheader:
pi = QStandardItem(header)
pm.setHorizontalHeaderItem(count, pi)
count+=1
countRow=0
for fileinfos in self.fileList:
firstColumn = QStandardItem(QIcon(self.fileList[countRow][2]), self.fileList[countRow][0])
secondColumn = QStandardItem(self.fileList[countRow][1])
pm.setItem(countRow,0,firstColumn)
pm.setItem(countRow,1,secondColumn)
countRow+=1
return pm
# Mise a jour des donnée du tableau
#========================================================================
def refreshTable(self):
pm = self.MyTableModel()
self.listView.setModel(pm)
self.listView.setIconSize(QSize(32, 32))
self.listView.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.listView.setItemDelegateForColumn(2,ProgressBarDelegate(self, self.fileList))
self.listView.setColumnWidth(0, 260)
self.listView.setColumnWidth(1, 200)
self.listView.setColumnWidth(2, 380)
class ProgressBarDelegate(QItemDelegate):
def __init__(self, owner, itemslist):
QItemDelegate.__init__(self, owner)
self.itemslist = itemslist
def sizeHint(self, option, index):
return QSize(120, 30)
def paint(self, painter, option, index):
opts = QStyleOptionProgressBarV2()
opts.rect = option.rect
opts.minimum = 1
opts.maximum = 100
percent = self.itemslist[index.row()][3]
opts.progress = percent
if percent == 0:
opts.textVisible = False
else:
opts.textVisible = True
opts.text = QString('%d%%'%percent)
QApplication.style().drawControl(QStyle.CE_ProgressBar, opts, painter)
#############################################################################
if __name__ == "__main__":
app = QApplication(sys.argv)
fen = mainWidget()
fen.setWindowTitle("TableView with Delegated ProgressBar")
fen.show()
sys.exit(app.exec_()) |
Partager