1

我有一些不寻常的问题:对于打包进度的可视化,我认为 qprogressbar 在一个栏中有两个值 - 一个显示读取的字节数,另一个显示写出的字节数,这也给出了关于压缩率的想象。

QT4可以吗?

另外,我对 C++ 编码的经验很少,我目前的工作是基于 Python、PyQT4、

4

1 回答 1

1

是的,这是可能的,但您必须实现自己的“DualValueProgressbar”,这里有一个示例,不是完整的生产代码,但它会为您指明正确的方向。

继续前的注意事项:

这样您将能够在栏中显示两个值,但是在同一个栏中显示两种颜色是完全不同的事情。所以我建议你使用两个进度条来做你想做的事,保持简单。

在看到任何代码之前,让我解释一下我做了什么。

  1. 子类QProgressBar
  2. 添加一个名为 的变量成员self.__value_1。这将是第二个值。
  3. 覆盖该方法paintEventself.__value_1在栏内绘制。

建议:

  1. 编写代码来建立第二个值的限制。(最小和最大)
  2. 编写处理该format属性的代码。
  3. 编写代码来处理对齐属性。

这是结果:

在此处输入图像描述

这是代码:

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())

最后是代码示例:

于 2014-06-25T20:24:32.643 回答