简体   繁体   中英

Strange behavior of QProgressBar with PyQt4

I have this sample of code:

import sys
import time
from PyQt4.QtGui import *
from PyQt4.QtCore import *

class Bar(QDialog):    
    def __init__(self, parent=None):
        super(Bar, self).__init__()        
        self.pbar = QProgressBar(self)
        self.pbar.setValue(0)
        layout = QHBoxLayout()
        layout.addWidget(self.pbar)
        self.setLayout(layout)

    def main(self):
        for value in range(1, 100):
            time.sleep(1)
            print value
            self.pbar.setValue(value)

app = QApplication(sys.argv)
form = Bar()
form.show()
form.main()
app.exec_()

I expect progressbar's value to increased by 1 every second. Instead, although that all values printed on the screen, the progressbar shows only some of them. Also, the bar appears just when value == 5. I know how to achieve the appropriate result with QBasicTimer(), but why this one does not work? Did i make a stupid mistake?

Try adding a

QApplication.processEvents()

just after print value (this should force the UI to update).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM