【发布时间】:2021-12-20 10:04:51
【问题描述】:
我尝试为 tableview 实现 ProgressBar 委托并偶然发现了这个示例:
How to include a column of progress bars within a QTableView?
tableview/PyQt5 的示例与 PyQt5 完美配合。
但是尝试使用 PySide6 会在第一行产生一个故障进度条,并在所有其他行产生灰色框。 PyQt6 根本不起作用,即使添加了完全限定的名称。
代码如下:
gui = "PySide6"
#gui = "PyQt5"
#gui = "PyQt6"
if gui =="PyQt5":
from PyQt5.QtWidgets import QStyle, QStyledItemDelegate, QStyleOptionProgressBar,\
QApplication, QTableView
from PyQt5.QtGui import QStandardItem, QStandardItemModel
from PyQt5.QtCore import Qt
if gui == "PySide6":
from PySide6.QtWidgets import QStyle, QStyledItemDelegate, QStyleOptionProgressBar,\
QApplication, QTableView
from PySide6.QtGui import QStandardItem, QStandardItemModel
from PySide6.QtCore import Qt
if gui == "PyQt6":
from PyQt6.QtWidgets import QStyle, QStyledItemDelegate, QStyleOptionProgressBar,\
QApplication, QTableView
from PyQt6.QtGui import QStandardItem, QStandardItemModel
from PyQt6.QtCore import Qt
data = [("1", "abc", 10), ("2", "def", 60),
("3", "ghi", 20), ("4", "jkl", 80),
("5", "mno", 100)]
class ProgressDelegate(QStyledItemDelegate):
def paint(self, painter, option, index):
progress = index.data(Qt.ItemDataRole.UserRole+1000)
opt = QStyleOptionProgressBar()
opt.rect = option.rect
opt.minimum = 0
opt.maximum = 100
opt.progress = progress
opt.text = f"{progress}%"
opt.textVisible = True
QApplication.style().drawControl(QStyle.CE_ProgressBar, opt, painter)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = QTableView()
delegate = ProgressDelegate(w)
w.setItemDelegateForColumn(2, delegate)
model = QStandardItemModel(0, 3)
model.setHorizontalHeaderLabels(["ID", "Name", "Progress"])
for _id, _name, _progress in data:
it_id = QStandardItem(_id)
it_name = QStandardItem(_name)
it_progress = QStandardItem()
it_progress.setData(_progress, Qt.ItemDataRole.UserRole+1000)
model.appendRow([it_id, it_name, it_progress])
w.setModel(model)
w.show()
app.exec()
谁有办法解决这个问题?
【问题讨论】:
标签: python pyqt5 delegates pyside6