Python 进度条代理垂直填充,而不是水平填充
我正在尝试使用带有自定义绘制方法的Python 进度条代理垂直填充,而不是水平填充,python,qt,pyside,qtwidgets,Python,Qt,Pyside,Qtwidgets,我正在尝试使用带有自定义绘制方法的QStylesItemDelegate在QTreeWidget中绘制列,以显示进度条的外观 但是,进度条从下到上填充,请参见所附屏幕截图(同时,文本未显示!): 相反,我希望它从左到右填充,我相信这应该通过QStyleOptionProgressBar.direction 以下是制作我的屏幕截图的MRE: import sys from PySide6 import ( QtCore, QtWidgets ) class MyDelegat
QStylesItemDelegate
在QTreeWidget
中绘制列,以显示进度条的外观
但是,进度条从下到上填充,请参见所附屏幕截图(同时,文本未显示!):
相反,我希望它从左到右填充,我相信这应该通过QStyleOptionProgressBar.direction
以下是制作我的屏幕截图的MRE:
import sys
from PySide6 import (
QtCore,
QtWidgets
)
class MyDelegate(QtWidgets.QStyledItemDelegate):
def paint(self, painter, option, index):
progress_bar_option = QtWidgets.QStyleOptionProgressBar()
progress_bar_option.rect = option.rect
progress_bar_option.state = QtWidgets.QStyle.State_Enabled
progress_bar_option.direction = QtCore.Qt.LayoutDirection.LeftToRight
progress_bar_option.fontMetrics = QtWidgets.QApplication.fontMetrics()
progress_bar_option.minimum = 0
progress_bar_option.maximum = 100
progress_bar_option.textAlignment = QtCore.Qt.AlignCenter
progress_bar_option.textVisible = True
progress_bar_option.progress = 66
progress_bar_option.text = 'demo'
QtWidgets.QApplication.style().drawControl(QtWidgets.QStyle.CE_ProgressBar,
progress_bar_option, painter)
class MyWidget(QtWidgets.QTreeWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.examplerow = QtWidgets.QTreeWidgetItem(self)
self.setHeaderLabels(['Col 1', 'Col 2', 'Col 3'])
self.setAlternatingRowColors(True)
self.examplerow.setText(0, 'Content in first column')
self.examplerow.setText(1, 'second')
self.examplerow.setText(2, str(3))
delegate = MyDelegate(self)
self.setItemDelegateForColumn(2, delegate)
if __name__ == "__main__":
app = QtWidgets.QApplication()
widget = MyWidget()
window = QtWidgets.QMainWindow()
window.setCentralWidget(widget)
window.resize(800, 600)
window.show()
sys.exit(app.exec_())
更改进度条方向的正确方法是什么?变量
方向
与进度条方向无关,因为它对于所有QStyleOption类都是通用的,并且与文本布局方向相关(对于希伯来语或阿拉伯语等语言,从左到右,或从右到左)
您需要的是orientation
变量,该变量自Qt 5.5以来就被认为是过时的,支持适当的QStyle.State
标志:
# ...
progress_bar_option.state = QtWidgets.QStyle.State_Enabled
progress_bar_option.state |= QtWidgets.QStyle.State_Horizontal
尝试添加
QStyle.State\u Horizontal
标志:progress\u bar\u option.State=qtwidts.QStyle.State\u Enabled | qtwidts.QStyle.State\u Horizontal
效果很好。请你把你的评论转换成回答好吗?