我想在QLabel小部件中显示.gif动画以及文本.
以下代码不起作用:
self.status_txt = QtGui.QLabel('Loading... <img src="etc/loading.gif">')
因为图像不会动画.
我尝试使用QMovie对象来实现它:
self.status_txt = QtGui.QLabel("Loading...") movie = QtGui.QMovie("etc/loading.gif") self.status_txt.setMovie(movie) movie.start()
@R_301_323@
你可以添加一个布局到标签,然后添加另一个标签与文本…
self.status_txt = QtGui.QLabel() movie = QtGui.QMovie("etc/loading.gif") self.status_txt.setMovie(movie) movie.start() self.status_txt.setLayout(QtGui.QHBoxLayout()) self.status_txt.layout().addWidget(QLabel('Loading...'))
编辑:
如果您使用自己版本的QLabel和QPainter自行绘制文本,则可以:
from PyQt4.QtCore import QSize from PyQt4.QtGui import QApplication,QLabel,QMovie,QPainter,QFontMetrics class QTextMovieLabel(QLabel): def __init__(self,text,fileName): QLabel.__init__(self) self._text = text m = QMovie(fileName) m.start() self.setMovie(m) def setMovie(self,movie): QLabel.setMovie(self,movie) s=movie.currentImage().size() self._movieWidth = s.width() self._movieHeight = s.height() def paintEvent(self,evt): QLabel.paintEvent(self,evt) p = QPainter(self) p.setFont(self.font()) x = self._movieWidth + 6 y = (self.height() + p.fontMetrics().xHeight()) / 2 p.drawText(x,y,self._text) p.end() def sizeHint(self): fm = QFontMetrics(self.font()) return QSize(self._movieWidth + 6 + fm.width(self._text),self._movieHeight) def setText(self,text): self._text = text if __name__ == '__main__': import sys app = QApplication(sys.argv) l = QTextMovieLabel('Loading...','loading.gif') l.show() app.exec_()