from PyQt5 import QtWidgets, QtCore, QtGui
import sys, time
sigStatusChanged = QtCore.pyqtSignal(int)
sigTaskFinished = QtCore.pyqtSignal()
def __init__(self):
def task(self):
for n in range(10):
time.sleep(0.1)
self.sigStatusChanged.emit(n)
print('thread : ', int(self.thread().currentThreadId()))
self.sigTaskFinished.emit()
sigStatusChanged = QtCore.pyqtSignal(int)
sigTaskFinished = QtCore.pyqtSignal()
def __init__(self, parent):
QtCore.
QThread.__init__
(self, parent
)
def run(self):
for n in range(10):
time.sleep(0.1)
self.sigStatusChanged.emit(n)
print('thread : ', int(self.thread().currentThreadId()))
self.sigTaskFinished.emit()
def __init__(self):
self.setWindowTitle("Test de QThread")
self.statusBar().showMessage('Welcome', 1000)
self.mdiArea = QtWidgets.QMdiArea()
self.setCentralWidget(self.mdiArea)
self.lbl = None
fileMenu = self.menuBar().addMenu('File')
threadAction
= QtWidgets.
QAction('Thread', self
) #threadAction.triggered.connect(self.threadWithWorker)
threadAction.triggered.connect(self.threadWithThread)
addWidgetAction
= QtWidgets.
QAction('Add Widget', self
) addWidgetAction.triggered.connect(self.addWidget)
removeWidgetAction
= QtWidgets.
QAction('Remove Widget', self
) removeWidgetAction.triggered.connect(self.removeWidget)
fileMenu.addAction(threadAction)
fileMenu.addAction(addWidgetAction)
fileMenu.addAction(removeWidgetAction)
print('app : ', int(self.thread().currentThreadId()))
def threadWithWorker(self):
def stopThread():
thread.quit()
thread.wait()
thread.start()
worker = Worker()
worker.moveToThread(thread)
worker.sigTaskFinished.connect(stopThread)
worker.sigStatusChanged.connect(self.updateStatus)
worker.task()
def threadWithThread(self):
thread = Thread(self)
thread.sigStatusChanged.connect(self.updateStatus)
thread.start()
def updateStatus(self, val):
if self.lbl is not None:
self.lbl.setText(str(val))
def addWidget(self):
self.
lbl = QtWidgets.
QLabel('Test') self.statusBar().addWidget(self.lbl)
def removeWidget(self):
if self.lbl is not None:
self.statusBar().removeWidget(self.lbl)
if __name__ == '__main__':
view = MainWindow()
view.show()
app.exec_()
from PyQt5 import QtWidgets, QtCore, QtGui
import sys, time
class Worker(QtCore.QObject):
sigStatusChanged = QtCore.pyqtSignal(int)
sigTaskFinished = QtCore.pyqtSignal()
def __init__(self):
QtCore.QObject.__init__(self)
def task(self):
for n in range(10):
time.sleep(0.1)
self.sigStatusChanged.emit(n)
print('thread : ', int(self.thread().currentThreadId()))
self.sigTaskFinished.emit()
class Thread(QtCore.QThread):
sigStatusChanged = QtCore.pyqtSignal(int)
sigTaskFinished = QtCore.pyqtSignal()
def __init__(self, parent):
QtCore.QThread.__init__(self, parent)
def run(self):
for n in range(10):
time.sleep(0.1)
self.sigStatusChanged.emit(n)
print('thread : ', int(self.thread().currentThreadId()))
self.sigTaskFinished.emit()
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
self.setWindowTitle("Test de QThread")
self.statusBar().showMessage('Welcome', 1000)
self.mdiArea = QtWidgets.QMdiArea()
self.setCentralWidget(self.mdiArea)
self.lbl = None
fileMenu = self.menuBar().addMenu('File')
threadAction = QtWidgets.QAction('Thread', self)
#threadAction.triggered.connect(self.threadWithWorker)
threadAction.triggered.connect(self.threadWithThread)
addWidgetAction = QtWidgets.QAction('Add Widget', self)
addWidgetAction.triggered.connect(self.addWidget)
removeWidgetAction = QtWidgets.QAction('Remove Widget', self)
removeWidgetAction.triggered.connect(self.removeWidget)
fileMenu.addAction(threadAction)
fileMenu.addAction(addWidgetAction)
fileMenu.addAction(removeWidgetAction)
print('app : ', int(self.thread().currentThreadId()))
def threadWithWorker(self):
def stopThread():
thread.quit()
thread.wait()
thread = QtCore.QThread()
thread.start()
worker = Worker()
worker.moveToThread(thread)
worker.sigTaskFinished.connect(stopThread)
worker.sigStatusChanged.connect(self.updateStatus)
worker.task()
def threadWithThread(self):
thread = Thread(self)
QtCore.QCoreApplication.processEvents()
thread.sigStatusChanged.connect(self.updateStatus)
thread.start()
def updateStatus(self, val):
if self.lbl is not None:
self.lbl.setText(str(val))
def addWidget(self):
self.lbl = QtWidgets.QLabel('Test')
self.statusBar().addWidget(self.lbl)
def removeWidget(self):
if self.lbl is not None:
self.statusBar().removeWidget(self.lbl)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
view = MainWindow()
view.show()
app.exec_()
To copy to clipboard, switch view to plain text mode
Bookmarks