我有一些不寻常的问题: 为了可视化打包进度,我考虑使用 qprogressbar 在一个栏中包含两个值 - 一个显示读取的字节,另一个显示写出的字节,这也给出了关于压缩比的想象。
QT4 可以吗?
另外,我对 C++ 编码的经验很少,我目前的工作是基于 Python、PyQT4,
最佳答案
是的,这是可能的,但您必须实现自己的“DualValueProgressbar”,这里有一个示例,不是完整的生产代码,但它会为您指明正确的方向。
继续之前的说明:
这样您将能够在栏中显示两个值,但在同一栏中显示两种颜色是非常不同的事情。所以我建议你使用两个 prograssbar 来做你想做的事情,保持简单。
在看任何代码之前,让我解释一下我做了什么。
- 子类
QProgressBar
- 添加一个名为
self.__value_1
的变量成员。这将是第二个值。 - 重写方法
paintEvent
以在栏中绘制self.__value_1
。
建议:
- 编写代码来建立第二个值的限制。 (最小值和最大值)
- 编写代码来处理
format
属性。 - 编写代码以控制对齐属性。
这是结果:
这是代码:
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class DualValueProgressBar(QProgressBar):
def __init__(self, parent=None):
super(DualValueProgressBar, self).__init__(parent)
# The other value you want to show
self.__value_1 = 0
def paintEvent(self, event):
# Paint the parent.
super(DualValueProgressBar, self).paintEvent(event)
# In the future versions if your custom object you
# should use this to set the position of the value_1
# in the progressbar, right now I'm not using it.
aligment = self.alignment()
geometry = self.rect() # You use this to set the position of the text.
# Start to paint.
qp = QPainter()
qp.begin(self)
qp.drawText(geometry.center().x() + 20, geometry.center().y() + qp.fontMetrics().height()/2.0, "{0}%".format(str(self.value1)))
qp.end()
@property
def value1(self):
return self.__value_1
@pyqtSlot("int")
def setValue1(self, value):
self.__value_1 = value
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = QWidget()
hlayout = QHBoxLayout(window)
dpb = DualValueProgressBar(window)
dpb.setAlignment(Qt.AlignHCenter)
# This two lines are important.
dpb.setValue(20)
dpb.setValue1(10) # Look you can set another value.
hlayout.addWidget(dpb)
window.setLayout(hlayout)
window.show()
sys.exit(app.exec())
最后是代码示例:
关于python - 有两个值的 Qprogressbar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24134310/