Results 1 to 4 of 4

Thread: QMovie from inside a custom item delegate

  1. #1
    Join Date
    May 2008
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QMovie from inside a custom item delegate

    Hi,

    I wonder if it's possible to embed an animated image (preferably scaled) into a QTreeView cell.

    So far I have achieved it by creating QMovie in the model and connecting its frameChanged() signal to model's dataChanged() signal. It turned out to be really CPU heavy as compared to QLabel with movie set into it.

    Could you please suggest a better way to do it?

  2. #2
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QMovie from inside a custom item delegate

    Quote Originally Posted by Crazy_Hopper View Post
    Hi,

    I wonder if it's possible to embed an animated image (preferably scaled) into a QTreeView cell.

    So far I have achieved it by creating QMovie in the model and connecting its frameChanged() signal to model's dataChanged() signal. It turned out to be really CPU heavy as compared to QLabel with movie set into it.

    Could you please suggest a better way to do it?
    Hmm interesting use case...
    As far as i know, the model's data changed signal is supposed to be emitted only if the data changes. In your case not each frame can be regarded as data. Model should only set the right QMovie for the delagate and that delegate should handle its painting as and when requested by the QMovie.

    You can use the techniques adopted by QLabel for playing the movie, which is nothing but painting the pixmaps as and when requested by QMovie. I am pasting here for quick reference.

    Qt Code:
    1. void QLabel::setMovie(QMovie *movie)
    2. {
    3. Q_D(QLabel);
    4. d->clearContents();
    5.  
    6. if (!movie)
    7. return;
    8.  
    9. d->movie = movie;
    10. connect(movie, SIGNAL(resized(QSize)), this, SLOT(_q_movieResized(QSize)));
    11. connect(movie, SIGNAL(updated(QRect)), this, SLOT(_q_movieUpdated(QRect)));
    12.  
    13. // Assume that if the movie is running,
    14. // resize/update signals will come soon enough
    15. if (movie->state() != QMovie::Running)
    16. d->updateLabel();
    17. }
    18.  
    19. void QLabelPrivate::_q_movieUpdated(const QRect& rect)
    20. {
    21. Q_Q(QLabel);
    22. if (movie && movie->isValid()) {
    23. QRect r;
    24. if (scaledcontents) {
    25. QRect cr = q->contentsRect();
    26. QRect pixmapRect(cr.topLeft(), movie->currentPixmap().size());
    27. if (pixmapRect.isEmpty())
    28. return;
    29. r.setRect(cr.left(), cr.top(),
    30. (rect.width() * cr.width()) / pixmapRect.width(),
    31. (rect.height() * cr.height()) / pixmapRect.height());
    32. } else {
    33. r = q->style()->itemPixmapRect(q->contentsRect(), align, movie->currentPixmap());
    34. r.translate(rect.x(), rect.y());
    35. r.setWidth(qMin(r.width(), rect.width()));
    36. r.setHeight(qMin(r.height(), rect.height()));
    37. }
    38. q->update(r);
    39. }
    40. }
    41.  
    42. void QLabelPrivate::_q_movieResized(const QSize& size)
    43. {
    44. Q_Q(QLabel);
    45. valid_hints = false;
    46. _q_movieUpdated(QRect(QPoint(0,0), size));
    47. q->updateGeometry();
    48. }
    49. void QLabel::paintEvent(QPaintEvent *)
    50. {
    51. Q_D(QLabel);
    52. QStyle *style = QWidget::style();
    53. QPainter painter(this);
    54. drawFrame(&painter);
    55. QRect cr = contentsRect();
    56. cr.adjust(d->margin, d->margin, -d->margin, -d->margin);
    57. int align = QStyle::visualAlignment(layoutDirection(), QFlag(d->align));
    58.  
    59. #ifndef QT_NO_MOVIE
    60. if (d->movie) {
    61. if (d->scaledcontents)
    62. style->drawItemPixmap(&painter, cr, align, d->movie->currentPixmap().scaled(cr.size()));
    63. else
    64. style->drawItemPixmap(&painter, cr, align, d->movie->currentPixmap());
    65. }
    66.  
    67. ... snip...
    To copy to clipboard, switch view to plain text mode 
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  3. #3
    Join Date
    May 2008
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QMovie from inside a custom item delegate

    Quote Originally Posted by Gopala Krishna View Post
    Hmm interesting use case...
    As far as i know, the model's data changed signal is supposed to be emitted only if the data changes. In your case not each frame can be regarded as data. Model should only set the right QMovie for the delagate and that delegate should handle its painting as and when requested by the QMovie.

    You can use the techniques adopted by QLabel for playing the movie, which is nothing but painting the pixmaps as and when requested by QMovie.
    With QLabel, QMovie generates a signal, it's processed and update() is called on QLabel which generates the paintEvent() for QLabel. It seems I do pretty much the same here. Besides, I guess there is no other way to call delegate's paint() but calling model's dataChanged().

    What I need is a code excerpt for delegate's paint() that shows a movie.

  4. #4
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QMovie from inside a custom item delegate

    Quote Originally Posted by Crazy_Hopper View Post
    With QLabel, QMovie generates a signal, it's processed and update() is called on QLabel which generates the paintEvent() for QLabel. It seems I do pretty much the same here. Besides, I guess there is no other way to call delegate's paint() but calling model's dataChanged().

    What I need is a code excerpt for delegate's paint() that shows a movie.
    Oh ok now i realize.
    How about updating the movie delegate from the QTreeView manually ? May be you can somehow find the rect corresponding to the delegate and keep issuing update(QRect) for the QTreeView on signal from the QMovie.
    What my idea is to avoid dataChanged signal in such a rapid rate since that results in quite expensive operations in the internal structures of InterView.
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

Similar Threads

  1. list of custom item views?
    By Beluvius in forum Qt Programming
    Replies: 1
    Last Post: 29th March 2008, 06:43

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.