gpt4 book ai didi

python - PyQt QProgressBar 垂直文本对齐问题

转载 作者:行者123 更新时间:2023-12-04 16:10:08 25 4
gpt4 key购买 nike

我正在尝试改变垂直对齐的 QProgressBar 的颜色

我在这里找到了如何通过 setStyleSheet 改变颜色的例子: Changing the color of a QProgressbar()

但是没有提到垂直对齐。请检查我的示例的屏幕截图:

第一个进度条是默认的,第二个和第三个是改变颜色的。但是文本没有正确对齐(我需要它作为第一个默认栏)。

我也尝试了 setTextDirection(QtGui.QProgressBar.TopToBottom) 但它没有帮助。

请指教

试用代码:

import sys
from PyQt4 import QtGui, QtCore

class Window(QtGui.QMainWindow):

def __init__(self):
super(Window, self).__init__()
self.setGeometry(50, 50, 500, 300)

progress1 = QtGui.QProgressBar(self)
progress1.setGeometry(50, 50, 25, 150)
progress1.setOrientation(QtCore.Qt.Vertical)
progress1.setMaximum(0)
progress1.setMaximum(10)
progress1.setValue(4)
progress1.setFormat("myServer008-Load")

progress2 = QtGui.QProgressBar(self)
progress2.setGeometry(100, 50, 25, 150)
# adding style will stop rotating text 90 degrees clockwise
progress2.setStyleSheet("QProgressBar::chunk { background-color: red; }")
progress2.setOrientation(QtCore.Qt.Vertical)
progress2.setMaximum(0)
progress2.setMaximum(10)
progress2.setValue(4)
progress2.setFormat("myServer008-Load")

progress3 = QtGui.QProgressBar(self)
progress3.setGeometry(150, 50, 25, 150)
# centring text works, but still do no rotate it
progress3.setStyleSheet("QProgressBar { text-align: center; } QProgressBar::chunk { background-color: red; }")
progress3.setOrientation(QtCore.Qt.Vertical)
progress3.setMaximum(0)
progress3.setMaximum(10)
progress3.setValue(4)
progress3.setFormat("myServer008-Load")


app = QtGui.QApplication(sys.argv)
GUI = Window()
GUI.show()
sys.exit(app.exec_())

最佳答案

我找到了绘制自己的垂直进度条的解决方法。

也许有人也可以使用这个:

class MyBar(QtGui.QWidget):
""" Creates custom 'vertical progress bar'"""

def __init__(self, text, maximumValue, currentValue, parent=None):
super(MyBar, self).__init__(parent)
self.text = text
self.maximumValue = maximumValue
self.currentValue = currentValue

def setValue(self, currentValue):
if self.currentValue != currentValue:
self.currentValue = currentValue
self.repaint()

def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.translate(0, self.height() - 1)
painter.rotate(-90)

painter.setPen(QtGui.QColor(140, 138, 135))
painter.drawRoundedRect(QtCore.QRectF(0, 0, self.height() - 1, self.width() - 1), 4, 4)

painter.setPen(QtGui.QColor(201, 199, 197))
path = QtGui.QPainterPath()
path.addRoundedRect(QtCore.QRectF(1, 1, self.height() - 3, self.width() - 3), 3, 3)

painter.fillPath(path, QtGui.QColor(214, 212, 210))
painter.drawPath(path)

path = QtGui.QPainterPath()
path.addRoundedRect(QtCore.QRectF(1, 1, (self.height() - 3) * self.currentValue / self.maximumValue, self.width() - 3), 3, 3)

painter.fillPath(path, QtGui.QColor(255, 0, 0))
painter.drawPath(path)

painter.setPen(QtCore.Qt.black)
# print text centered
painter.drawText(5, self.width() / 2 + 5, self.text)
painter.end()

然后在问题代码中简单地添加:

progress4 = MyBar("myServer008-Load", 10, 4, self)
progress4.setGeometry(200, 50, 25, 150)

结果如下所示:

result example

关于python - PyQt QProgressBar 垂直文本对齐问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43952242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com