Milan Uhrák
Milan Uhrák

Reputation: 9

Qprogressbar with two values

I have some unusual question : For visualization of packing progress i think about qprogressbar with two values in one bar - one showing bytes read, and another showing write-out bytes, which gives also imagine about compress ratio.

It is possible with QT4 ?

Also, I have very little experience with C++ coding, my current work is based on Python, PyQT4,

Upvotes: 1

Views: 174

Answers (1)

Raydel Miranda
Raydel Miranda

Reputation: 14370

Yes it's possible, but you will have to implement your own "DualValueProgressbar" here you have an example, is not complete production code but it will point to you in the right direction.

A note before continue:

Will this you will be able to show two values in the bar, but show two colours in the same bar is a very different thing. So I'll recomend you to use two prograssbar for doing what you want, keep it simple.

Before see any code let me explain what I did.

  1. Subclass QProgressBar
  2. Add a variable member called self.__value_1. This will be the second value.
  3. Override the method paintEvent in order to draw self.__value_1 inside the bar.

Recomendations:

  1. Write code for establishing limits on the second value. (Minimun and maximun)
  2. Write code for handle the format property.
  3. Write code for habdle the aligment property.

This is the result:

enter image description here

Here is the code:

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

Finally the code sample:

Upvotes: 1

Related Questions