During handling of a clicked
signal I would like to show a wait cursor until this is done.
All mouse clicks on other widgets should be ignored during that time.
The cursor part is working fine using setCursor()
.
But all mouse clicks on other buttons (or the original one) are still detected and cached, so they are handled after the first button operation is finished.
How can I make sure that no user interface events are generated until my operation is finished?
This code shows the things I have already tried:
pushButton_B.setEnabled()
pushButton_B.clicked.disconnect()
pushButton_B.blockSignals()
None of those have led to the desired behaviour.
I am hoping for some way to disable user interface events for the whole window, so I don't need to do this for every widget I introduce, but I couldn't find the right methods for this.
I'd appreciate any hints on how to do this properly.
import sys
import time
from PySide6.QtCore import Qt, QRect, QCoreApplication, QMetaObject
from PySide6.QtWidgets import QApplication, QWidget, QMainWindow, QPushButton
class Ui_MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.resize(200, 200)
self.centralwidget = QWidget(self)
self.pushButton_A = QPushButton(self.centralwidget)
self.pushButton_A.setText('A')
self.pushButton_A.setGeometry(QRect(20, 20, 100, 24))
self.pushButton_B = QPushButton(self.centralwidget)
self.pushButton_B.setText('B')
self.pushButton_B.setGeometry(QRect(20, 60, 100, 24))
self.setCentralWidget(self.centralwidget)
self.pushButton_A.clicked.connect(self.pushButton_A_clicked)
self.pushButton_B.clicked.connect(self.pushButton_B_clicked)
def pushButton_A_clicked(self):
print("pushButton_A clicked...")
self.setCursor(Qt.CursorShape.WaitCursor)
self.pushButton_B.setEnabled(False)
self.pushButton_B.clicked.disconnect(self.pushButton_B_clicked)
self.pushButton_B.blockSignals(True)
self.repaint()
time.sleep(3) # Some lengthy operation
self.setCursor(Qt.CursorShape.ArrowCursor)
self.pushButton_B.setEnabled(True)
self.pushButton_B.clicked.connect(self.pushButton_B_clicked)
self.pushButton_B.blockSignals(False)
print("pushButton_A done.")
def pushButton_B_clicked(self):
print("pushButton_B clicked.")
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Ui_MainWindow()
w.show()
app.exec()