PyQt と Qthreads を使用して GUI の応答性を維持することに問題があります。私がしているのは、CPU が重い GUI からワーカー Qthread を生成することです。ワーカー Qthread は、数値を含むシグナルをサイクルごとに送信します。GUI では、信号は数値を取得して GUI を再描画する更新関数に接続されます。標準の Qt SIGNAL/SLOT メカニズムを使用しています。
これはすべて、PyQt、GUI、および Qthreads に関してオンラインで見つけたドキュメントと一致しているようです。問題は、ワーカー スレッドがシグナルを頻繁にトリガーするため、GUI が非常に遅くなることです。解決策は、ワーカースレッドがシグナルをトリガーする頻度を減らすことですが、進行状況をすばやく更新したいと思います。
これはすべて非常に標準的なもののようですが、適切な解決策が見つかりません。ひょっとして、デザインを変える必要があるのでしょうか?
どんな助けでも大歓迎です。
コード:
import calculator as ca
class GUI(QMainWindow):
def __init__(self, config):
QMainWindow.__init__(self)
self.w_thread = WorkerThread()
self.connect(self.w_thread, SIGNAL("update(PyQt_PyObject)"), self.update_gui)
def start_calc(self):
self.w_thread.start()
def update_gui(self, progress_value):
self.progress_value = progress_value
self.repaint()
def paintEvent(self, event):
paint = QPainter()
paint.begin(self)
paint.drawText(300, 300, "progress: " + str(self.progress_value))
paint.end()
class WorkerThread(QThread):
def __init__(self):
QThread.__init__(self)
def __del__(self):
self.wait()
def run(self):
ca.run_calculations(self)
*************** calculator.py ***************
def run_calculations(thread=None):
while current_cycle < total_cycles:
do_calculations()
if current_cycle % 100 == 0:
thread.emit(SIGNAL("update(PyQt_PyObject)"), current_progress_value)
qApp.processEvents()
current_cycle += 1