QT 에서 UI thread 외에 worker thread 를 사용하는 예제
자세한 설명은 ref. 1 에서 확인하자. 여기서는 간단한 예제를 하나 제시하는 것으로 대신한다.
str 을 parameter 로 넘길때
되도록 python3 string 을 사용하자. 아래 literals 을 사용하면 된다.from __future__ import unicode_literals # at top of module
#coding=utf-8...param = unicode("한글".decode('utf8'))
QThread Worker 예제
class Worker(QThread):
"""
https://wiki.python.org/moin/PyQt/Threading,_Signals_and_Slots
"""
updateUi = pyqtSignal(str)
def __init__(self, parent = None):
QThread.__init__(self, parent)
self.exiting = False
def __del__(self):
self.exiting = True
self.wait()
def getNews(self):
self.start() # The start() method is a special method that sets up the thread and calls our implementation of the run() method.
def run(self):
while True:
if i == 5:
return
self.updateUi.emit(unicode(i))
class MyQTApplication(QtGui.QWidget):
DELIMITER = '$$'
def __init__(self, service):
super(MyQTApplication, self).__init__()
self.service = service
self.edtNewUpdates = QTextEdit()
self.btnNewUpdates = QPushButton()
self.thread1 = Worker()
self.initThread()
# to call thread's function
self.btnNewUpdates.clicked.connect(self.thread1.getNews)
def initThread(self):
# BIND thread 1's finish, terminated ... signals to self.update* methods
self.thread1.finished.connect(self.updateUiNewUpdates)
self.thread1.terminated.connect(self.updateUiNewUpdates)
self.thread1.updateUi.connect(self.updateUiOneNewUpdate)
# OLD codes for reference
# self.connect(self.thread1, SIGNAL("finished()"), self.updateUiNewUpdates)
# self.connect(self.thread1, SIGNAL("terminated()"), self.updateUiNewUpdates)
# self.connect(self.thread1, SIGNAL("updateUi(a)"), self.updateUiOneNewUpdate)
def updateUiNewUpdates(self, urls):
# write to output at once
self.edtNewUpdates.appendPlainText(unicode(str('end'))
def updateUiOneNewUpdate(self, number):
# write to output at once
self.edtNewUpdates.appendPlainText(unicode(str(number))
def main():
app = QtGui.QApplication(sys.argv)
service, flags = blogger.getService(sys.argv)
ex = MyQTApplication(service)
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Reference
- PyQt/Threading,_Signals_and_Slots - Python Wiki
- New-style Signal and Slot Support — PyQt 4.11.4 Reference Guide
- Signals and Slots in PySide/ko - Qt Wiki
댓글 없음:
댓글 쓰기