我正在使用pyvistaqt
并希望在加载数据时显示进度条窗口。我在不使用pyvista
with 的情况下取得了成功PyQt
(请参阅此 SO 帖子),但是当我添加vtk
.
我认为某些东西仍在阻塞主线程,但我不知道是什么。进度条根本不会显示,或者如果显示,进度条中途会停止加载并停止响应。任何帮助将非常感激:
设置:
python 3.8.10
pyvista 0.32.1
qtpy 1.11.3
输出:
MRE
from pyvistaqt import MainWindow, QtInteractor
from qtpy import QtCore, QtGui, QtWidgets
class Worker(QtCore.QObject):
update = QtCore.Signal(int)
done = QtCore.Signal()
def __init__(self):
super().__init__()
def load(self):
for num in range(100):
for i in range(200000):
continue # Simulate long-running task
self.update.emit(num)
self.done.emit()
class Controller(object):
def __init__(self):
self.view = View(controller=self)
def on_load(self):
self.thread = QtCore.QThread()
self.worker = Worker()
self.worker.moveToThread(self.thread)
self.view.show_progress_dialog()
self.thread.started.connect(lambda: self.worker.load())
self.worker.update.connect(self.view.progress_dialog.on_update)
def _on_finish():
self.view.hide_progress_dialog()
self.thread.quit()
self.worker.done.connect(_on_finish)
self.thread.finished.connect(self.worker.deleteLater)
self.thread.start()
class ProgressDialog(QtWidgets.QDialog):
def __init__(self, parent=None, title=None):
super().__init__(parent)
self.setWindowTitle(title)
self.pbar = QtWidgets.QProgressBar(self)
layout = QtWidgets.QVBoxLayout()
layout.addWidget(self.pbar)
self.setLayout(layout)
self.setWindowFlag(QtCore.Qt.WindowContextHelpButtonHint, False)
self.resize(500, 50)
self.hide()
def on_update(self, value):
self.pbar.setValue(value)
class View(MainWindow):
def __init__(self, controller):
super().__init__()
self.controller = controller
self.container = QtWidgets.QFrame()
self.layout_ = QtWidgets.QGridLayout()
self.layout_.setContentsMargins(0, 0, 0, 0)
self.container.setLayout(self.layout_)
self.setCentralWidget(self.container)
self.progress_dialog = ProgressDialog(self)
self.btn = QtWidgets.QPushButton(self)
self.btn.setText("Load")
self.btn.clicked.connect(self.controller.on_load)
def show_progress_dialog(self):
self.progress_dialog.setModal(True)
self.progress_dialog.show()
def hide_progress_dialog(self):
self.progress_dialog.hide()
self.progress_dialog.setModal(False)
self.progress_dialog.pbar.reset()
self.progress_dialog.title = None
if __name__ == "__main__":
app = QtWidgets.QApplication([])
root = Controller()
root.view.show()
app.exec_()