2

I'm learning Pyside QProgressBar on MacOSX. When I use QProgressBar like following, it only indicate 0% or 100%. How to make a QProgressBar smoothly? Is there any way to do this?

from PySide.QtGui import QApplication, QProgressBar, QWidget
from PySide.QtCore import QTimer
import time

app = QApplication([])
pbar = QProgressBar()
pbar.setMinimum(0)
pbar.setMaximum(100)

pbar.show()

def drawBar():
    global pbar
    pbar.update()

t = QTimer()
t.timeout.connect(drawBar)
t.start(100)

for i in range(1,101):
    time.sleep(0.1)
    pbar.setValue(i)

app.exec_()
Kei Minagawa
  • 4,395
  • 3
  • 25
  • 43

2 Answers2

4

Get rid of this code:

for i in range(1,101):   # this won't work, because
    time.sleep(0.1)      # Qt's event loop can't run while
    pbar.setValue(i)     # you are forcing the thread to sleep

and instead, add a global variable p:

p = 0

and increment it in your drawBar() function:

def drawBar():
    global pbar
    global p
    p = p + 1
    pbar.setValue(p)
    pbar.update()
Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
1

QPropertyAnimation is easy to use and it does the smooth change for you.

    animation = QtCore.QPropertyAnimation(pbar, "value")
    animation.setDuration(???)
    animation.setStartValue(0)
    animation.setEndValue(100)
    animation.start()

Edited post:

Just replace everything between pbar.show() and app.exec() by the code I suggested

Here is the complete code:

from PyQt5.QtWidgets import QWidget, QProgressBar, QApplication
from PyQt5.QtCore import QTimer, QPropertyAnimation

app = QApplication([])
pbar = QProgressBar()
pbar.setMinimum(0)
pbar.setMaximum(100)

pbar.show()

animation = QPropertyAnimation(pbar, "value")
animation.setDuration(2000)
animation.setStartValue(0)
animation.setEndValue(100)
animation.start()

app.exec_()
SauloT
  • 153
  • 7
  • This is difficult for me. Could you please give me entire code? – Kei Minagawa Jul 09 '14 at 05:22
  • inline `from PyQt5.QtWidgets import QWidget, QProgressBar, QApplication` from PyQt5.QtCore import QPropertyAnimation app = QApplication([]) pbar = QProgressBar() pbar.setMinimum(0) pbar.setMaximum(100) pbar.show() animation = QPropertyAnimation(pbar, "value") animation.setDuration(2000) animation.setStartValue(0) animation.setEndValue(100) animation.start() app.exec_() ` – SauloT Jul 09 '14 at 06:26
  • the comment box does not help formatting the code... Just replace everything between pbar.show() and app.exec() for the code I suggested – SauloT Jul 09 '14 at 06:35
  • Thanks. I changed `PyQt` to `PySide` and It worked. Ah I understand what you say, it's animation! – Kei Minagawa Jul 09 '14 at 06:45
  • @SauloT I suggest you extend/update your post instead of posting code in a comment. – Oliver Jul 09 '14 at 10:39
  • Can this be achieved with `QTimeLine()` as well? I, myself, have given up on `QPropertyAnimation()` since the transitions between `QProgressBar()` values were not smooth at all. Got any experience with `QTimeLine()`? – Boštjan Mejak Oct 26 '21 at 14:01