2

I would like to have a QMessageBox with a moving GIF as icon. So I made this:

from PyQt5.QtGui import * 
from PyQt5.QtWidgets import *
import os
import sys

def msg_wait(s):
    msg = QMessageBox()
    msg.setIconPixmap(QPixmap('wait.gif').scaledToWidth(100))
    msg.setText(s)
    msg.setWindowTitle(" ")
    msg.setModal(False)
    # msg.setStandardButtons(QMessageBox.Ok)
    msg.show()
    return msg


class SurfViewer(QMainWindow):
    def __init__(self, parent=None):
        super(SurfViewer, self).__init__()
        self.parent = parent
        self.centralWidget = QWidget()
        self.setCentralWidget(self.centralWidget)

        self.msg=msg_wait('blablabla')
        self.msg.setStandardButtons(QMessageBox.Cancel)

def main():
    app = QApplication(sys.argv)
    ex = SurfViewer(app)
    ex.setWindowTitle('NMM Stimulator')

    ex.showMaximized()
    ex.show()
    # ex.move(0, 0)
    # ex.resizescreen()
    sys.exit(app.exec_( ))


if __name__ == '__main__':
    main()

where the GIF is :

enter image description here

But the result is a fixed image. How can I make it animated? I tried something with the QMovie class but I wasn't able to set it in the QMessageBox.setIconPixmap function

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
ymmx
  • 4,769
  • 5
  • 32
  • 64

1 Answers1

5

QMovie must be used, but for this the first thing is to access the QLabel directly for it, use findChild:

def msg_wait(s):
    msg = QMessageBox()
    # create Label
    msg.setIconPixmap(QPixmap('wait.gif').scaledToWidth(100))
    icon_label = msg.findChild(QLabel, "qt_msgboxex_icon_label")
    movie = QMovie('wait.gif')
    # avoid garbage collector
    setattr(msg, 'icon_label', movie)
    icon_label.setMovie(movie)
    movie.start()

    msg.setText(s)
    msg.setWindowTitle(" ")
    msg.setModal(False)
    # msg.setStandardButtons(QMessageBox.Ok)
    msg.show()
    return msg
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • awesome ! this works perfectly. I have a question though. How could I find that in the documentation? If I look at the QMessageBox, I cannot find the qt_msgboxex_icon_label QLabel object. I mean how could I know that the QMessageBox had a QLabel child that I could modify? I have still so much to learn about Qt :) Again, a big thanks to you. – ymmx Apr 26 '18 at 07:23
  • @ymmx That is not in the documentation, the first thing is that you check the source code of Qt, and you understand the functioning of the widgets, for example you will see that some internal widgets are created in certain cases, and I see that by implicit convention your objectName will begin with qt_, and then it is to experiment and review where they are located and what they are for. – eyllanesc Apr 26 '18 at 17:55
  • Okay. Well I don't feel too confident to look at the source code yet. but indeed , it seems to be a good idea. – ymmx Apr 27 '18 at 08:07