I am trying to use qwaitingspinner.py (QtWaitingSpinner on GitHub) module in my project. The spinner's parent is an existing, visible QTabWidget page. When I add a page (a process whose initialization takes 5 to 10 seconds during which the 'tabVisible' attribute of the new page is kept at False), I start the spinner. This one is not displaying as I expect. However, it becomes visible and functional once the new page has been added and made visible (voluntarily, I don't stop the spinner to see what happens). I understand Python is busy executing the while loop in the example. And Qt's event loop also seems to be impacted since I don't see the spinner while the while loop is executing. So how to make the spinner functional while executing the loop in the example?
import sys
from time import monotonic
from PyQt6.QtWidgets import (
QApplication,
QMainWindow,
QPushButton,
QTabWidget,
QVBoxLayout,
QWidget,
)
from waitingspinnerwidget import QtWaitingSpinner
class MyWindow(QMainWindow):
def __init__(self):
super().__init__()
self.resize(400, 400)
self.setWindowTitle("Spinner test")
layout = QVBoxLayout()
self._tab = QTabWidget(self)
_page_1 = QWidget()
self._page_index = self._tab.addTab(_page_1, "Page with the spinner")
layout.addWidget(self._tab)
btn_add_page = QPushButton()
btn_add_page.setText("Add a page")
btn_add_page.clicked.connect(self.add_new_page)
layout.addWidget(btn_add_page)
widget = QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
self.spinner = None
def add_new_page(self):
_new_index = self._page_index + 1
widget = QWidget()
widget.setObjectName(f"page_{_new_index}")
self.start_spinner()
self._page_index = self._tab.addTab(widget, f"Page no {_new_index}")
self._tab.setTabVisible(self._page_index, False)
t = monotonic()
while monotonic() - t < 5.0:
# The purpose of this loop is to simulate time-consuming by the project constructor of a new page.
continue
self._tab.setTabVisible(self._page_index, True)
self._tab.setCurrentIndex(self._page_index)
self.stop_spinner()
def start_spinner(self):
self.spinner = QtWaitingSpinner(parent=self._tab.widget(self._tab.count() - 1))
self.spinner.start()