from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
class dlgTest( QDialog ):
def __init__( self, parent = None ):
QDialog.__init__( self, parent )
self.mainLayout = QVBoxLayout()
self.btnStart = QPushButton()
self.btnStart.setText("Start")
QObject.connect( self.btnStart, SIGNAL( "clicked()" ), self.startCalculation )
self.btnStop = QPushButton()
self.btnStop.setText("Stop")
QObject.connect( self.btnStop, SIGNAL( "clicked()" ), self.stopCalculation )
self.lblProgress = QLabel()
self.lblProgress.setText("Progress: 0")
self.mainLayout.addWidget(self.btnStart)
self.mainLayout.addWidget(self.btnStop)
self.mainLayout.addWidget(self.lblProgress)
self.setLayout(self.mainLayout)
self.setWindowTitle( self.tr( "Thread" ) )
self.resize( 120, 100 )
# запуск "анализа" в отдельном потоке
def startCalculation( self ):
self.threadCalc = workThread()
QObject.connect( self.threadCalc, SIGNAL( "runStatus(PyQt_PyObject)" ), self.runStatusFromThread )
self.threadCalc.start()
# остановка "анализа" по нажатию на кнопку
def stopCalculation( self ):
self.threadCalc.stop()
# для отображения процесса выполнения
def runStatusFromThread( self, status ):
self.lblProgress.setText( "Progress: " + str( status ) )
class workThread( QThread ):
def __init__( self, parent = None ):
QThread.__init__( self, parent )
self.running = False
self.counter = 0
def run( self ):
self.running = True
while True:
self.counter += 1
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), self.counter )
self.sleep(1)
# остановка потока
def stop( self ):
#self.counter = 0
self.running = False
if __name__ == "__main__":
app = QApplication( sys.argv )
frm = dlgTest()
frm.show()
sys.exit( app.exec_() )
P.S.: Если ошибся топиком, прошу модераторов извинить и поправить.