문제

I'm working on a program and I want to connect a QProgressBar with a function. While the function is in progress, the QProgressBar should count until the function is done. Then the QProgressBar should be done too.

도움이 되었습니까?

해결책

This sample will give you a simple idea of seeing the progress. By no way this is efficient or elegant. Its a just a working solution i rigged up.

#!/usr/bin/python

import os, sys

from time import sleep

from PyQt4.QtCore import *
from PyQt4.QtGui import *

app = QApplication( sys.argv )

def copyFile() :
    cpBtn.setDisabled( True )
    for i in range( 0, 100 ) :
        # File Copy Code
        # sleep( 0.1 ) is instead of the file copy code
        sleep( 0.1 )
        pb.setValue( i + 1 )
        qApp.processEvents()

    cpBtn.setEnabled( True )
    pb.reset()

fcpDlg = QDialog()

cpBtn = QPushButton( fcpDlg )
cpBtn.setText( "&Copy" )
cpBtn.clicked.connect( copyFile )
cpBtn.setFixedWidth( 72 )

pb = QProgressBar()
pb.setMinimumWidth( 300 )
pb.setRange( 0, 100 )

lyt = QVBoxLayout( fcpDlg )
lyt.addWidget( pb )
lyt.addWidget( cpBtn )

fcpDlg.setLayout( lyt )

fcpDlg.show()

sys.exit( app.exec_() )
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top