QLabel is not updated unless the mainWindow is unfocused
I am trying "Hello World" using PyQt5 with Python 3.7.3 and macOS 10.14.6. Executing pyqt_helloworld.py
below and clicking the button will update the label to "Hello World".
However, when the button is clicked, the text is not changed, and it is not until I focus on the window of other application manually that the label is updated. How can I update QLabel without unfocusing the PyQt application?
Thanks in advance!
pyqt_helloworld_ui.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_HelloWorld(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(70, 40, 201, 21))
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(130, 90, 113, 32))
self.pushButton.setObjectName("pushButton")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.label.setText(_translate("Dialog", "foobar"))
self.pushButton.setText(_translate("Dialog", "Click"))
pyqt_helloworld.py
import sys
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QMainWindow
from pyqt_helloworld_ui import Ui_HelloWorld
class HelloWorldGui(QMainWindow, Ui_HelloWorld):
def __init__(self, parent=None):
super(HelloWorldGui, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.setTextHelloWorld)
def setTextHelloWorld(self):
self.label.setText("Hello World")
if __name__ == '__main__':
argvs = sys.argv
app = QApplication(argvs)
hello_world_gui = HelloWorldGui()
hello_world_gui.show()
sys.exit(app.exec_())
the issue is present in PyQt5 since 5.11.0 (tested 5.11.x, 5.12.x and 5.13) and PySide2 v.5.13 on MacOS (tested 10.14 and 10.12.6). The v.5.10.1 works fine. The issue does not exist under Linux and Windows Adding a call to the repaint method fix the issue.
def setTextHelloWorld(self):
self.label.setText("Hello World")
self.label.repaint()