With PyQt4, why does setting a tabwidget tabbar icon widget to a label with an animated QMovie sometimes cause the animation to pause? [With Testcase]
I have a QMainWindow with a QTabWidget. From the QTabWidget, you can get a QTabBar and the set the QTabBar's TabButton as per https://doc.qt.io/archives/qt-4.8/qtabbar.html#setTabButton
So, I'd like to do this to put a throbber (ani开发者_高级运维mated gif) next to the text for each tab. This "usually" worked fine, until sometimes a peculiar behavior occured - namely, the animation would freeze and not update. Then, moving your mouse lets the animation update. Clearly there was as issue wit the event processing - if Qt has an event then the animation worked fine, but if not, it paused.
I've finally managed to get a reduced testcase for this problem after a days work, and it's listed below.
Copy the below code into a file, create an 'images' subfolder underneath it, and throw in a throbber.gif (say http://upload.wikimedia.org/wikipedia/en/7/78/Netscape_throbber_2.gif ).
Run the program, and notice how the throbber only animates when you move your mouse over the window. Next, change the line EVILNESS=10
to EVILNESS=1
and rerun the program. Notice now how the throbber animates continuously, even without moving your mouse.
Why is this? can I work around it? Am I doing something wrong?
My system:
Windows XP SP3 Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] PyQt 4.7.4
Thanks for the help / suggestions.
import os, sys
from PyQt4 import QtCore, QtGui
GOODNESS = 10
EVILNESS = 10
class MyWindow(QtGui.QMainWindow):
def __init__(self, app):
QtGui.QMainWindow.__init__(self)
self.app = app
self.initUI()
for i in range(GOODNESS):
self.addTab()
for i in range(EVILNESS):
self.animateTabIndex(self.tabMain, i, True)
def initUI(self):
self.centralwidget=QtGui.QWidget(self)
self.tabMain = QtGui.QTabWidget(self.centralwidget)
self.tabOne = QtGui.QWidget()
self.tabOne.edit = QtGui.QLineEdit(self.tabOne)
self.tabOne.edit.setText(QtCore.PYQT_VERSION_STR) #Qt Version
self.tabOneLayout = QtGui.QVBoxLayout(self.tabOne)
self.tabOneLayout.addWidget(self.tabOne.edit)
self.tabMain.addTab(self.tabOne, "First Tab")
self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)
self.verticalLayout.addWidget(self.tabMain)
self.setCentralWidget(self.centralwidget)
def addTab(self):
et = QtGui.QWidget()
someedit = QtGui.QLineEdit(et)
somelayout = QtGui.QVBoxLayout(et)
somelayout.addWidget(someedit)
self.tabMain.addTab(et, "Extra Tab")
def animateTabIndex(self, tabWidget, tabIndex, enable):
print tabIndex
tabBar = tabWidget.tabBar()
if enable:
lbl = QtGui.QLabel(tabWidget)
movie = QtGui.QMovie(os.path.join(self.app.basedir, "images\\throbber.gif"), parent=lbl)
movie.setCacheMode(QtGui.QMovie.CacheAll)
movie.setScaledSize(QtCore.QSize(16, 16))
lbl.setMovie(movie)
movie.start()
else:
lbl = QtGui.QLabel(tabWidget)
lbl.setMinimumSize(QtCore.QSize(16, 16))
tabBar.setTabButton(tabIndex, QtGui.QTabBar.LeftSide, lbl)
class MyApp(QtGui.QApplication):
basedir = os.path.dirname(__file__)
def __init__(self, args):
super(MyApp, self).__init__(args)
self.mainWindow = MyWindow(self)
def exec_(self):
QtCore.pyqtRemoveInputHook() #Needed to allow pdb, etc to work
ret = super(MyApp, self).exec_()
return ret
if __name__ == '__main__':
app = MyApp(sys.argv)
app.mainWindow.show()
app.exec_()
Well, this was apparently fixed by PyQt 4.8.2. Guess I should have tried upgrading earlier...
UPDATE: Specifically this: http:/bugreports.qt-project.org/browse/QTBUG-12721
精彩评论