How to load gif image in Qlistview

  • Hi ,
    I want to load the gif image in qlistview each row.Normally i used QMovie to load the gif image in widget.But How to load the listview each row.

    Kindly help me

  • Moderators

    you still have to use QMovie but it's a bit more tricky.

    You need to listen to QMovie's frameChanged() signal and trigger an update on the QListView using QAbstractItemView::update(const QModelIndex & index )
    In your model return the current frame of QMovie in the data() method for Qt::DecorationRole.

  • Thanks for your reply.Normally I fill the list strings by using custom delegate
    paint method.Now my question is how i use the QMovie inside the paint or custom delegate.?

    How i load the QMovie in row?


  • Moderators

    In your custom delegate do you call the base class implementation for painting or do you do the painting of the whole cell by yourself?

    In the first case do as i said with the model and the Qt::DecorationRole. In the second case just paint the current frame of the QMovie yourself.

    If you mean how to handle the QMovies:
    You will need to store them on a central place (e.g. model) with your view. You can use QPersistentModelIndex for mapping the index to the right QMovie object.
    But you need to take care of the cleanup of the list (or whatever storage container you use) and remove the QPersistentModelIndex and it's QMovie object from to avoid "infinite" memory consumption ;)

  • In paint method i used QPainter for drawText and drawPixmap functions


  • Moderators

    then you just need to do this:
    painter->drawPixmap( movie->currentPixmap() );

    since you connect to QMovie's frameChanged() signal as i said and trigger an update, the cell will be redrawn every time the gif animation image changes.
    Thus you will see animated gifs in a listview.

  • Ok.Thanks for your help.I will implement this and let you know.


Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.