-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow a background task to use the message bar #38
Comments
Probably not doable? Looks like message bar cannot be accessed from outside the main thread :( |
Will cause an Exception:unknown at https://github.com/GispoCoding/qgis_plugin_tools/blob/master/tools/custom_logging.py#L183 |
Background tasks can use feedback and the UI thread could use that feedback message to display messages on message bar 🤔 |
This is how I got it working. In the UI thread: @staticmethod
def pop_widget(mbi: QgsMessageBarItem) -> None:
if mbi in iface.messageBar().items():
iface.messageBar().popWidget(mbi)
@classmethod
def prepare_task(cls, task: QgsTask, message: str, maximum: Optional[int] = None) -> None:
msg_bar = iface.messageBar()
mbi_progress = msg_bar.createMessage(message)
progress_bar = QProgressBar(mbi_progress)
progress_bar.setAlignment(Qt.AlignLeft | Qt.AlignVCenter)
if maximum is not None:
progress_bar.setMaximum(maximum)
task.progressChanged.connect(progress_bar.setValue)
else:
progress_bar.setRange(0, 0)
mbi_progress.layout().addWidget(progress_bar)
msg_bar.pushWidget(mbi_progress, Qgis.Info)
btn_close: QToolButton = msg_bar.findChildren(QToolButton)[0]
for slot in (
functools.partial(cls.pop_widget, mbi_progress),
functools.partial(btn_close.clicked.disconnect, task.cancel),
):
task.taskCompleted.connect(slot)
task.taskTerminated.connect(slot)
btn_close.clicked.connect(task.cancel)
QgsApplication.taskManager().addTask(task) Then, in task's |
Add support for bar_msg in
setup_task_logger
. Will make a PR on this.The text was updated successfully, but these errors were encountered: