我正在使用PyQt5编写一个管理销售订单的应用程序。在创建或删除订单时,我想显示一个走马灯样式的进度对话框来指示应用程序正在工作。我查看了许多帖子,其中答案涉及使用QThread。我尝试实现它,但似乎我漏掉了一些东西。这是我的线程类。
class Worker(QThread):
finished = Signal()
def run(self):
self.x = QProgressDialog("Please wait..",None,0,0)
self.x.show()
def stop(self):
self.x.close()
在主窗口的init中,我创建了self.worker=Worker()。
现在删除条目的代码示例如下:
msg = MsgBox("yn", "Delete Order", "Are you sure you want to delete this order?") # Wrapper for the QMessageBox
if msg == 16384:
self.worker.start() ## start the worker thread, hoping to start the progress dialog
session.delete(order) ##delete order from db
session.commit() ##commit to db
self.change_view("Active", 8) ##func. clean up the table.
self.worker.finished.emit() ##emit the finished signal to close the progress dialog
无进度对话框显示。GUI 会在一两秒钟内冻结,然后条目删除而没有任何进度对话框显示。
抱歉,我的代码相当长,所以我无法在这里全部包含它,我只是想看看是否犯了什么严重的错误。
QThread.started
和QThread.finished
信号来显示/隐藏。 - Heike