Results 1 to 3 of 3

Thread: PNG-based animation

  1. #1
    Join Date
    Jan 2006
    Location
    germany
    Posts
    75
    Thanks
    9
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default PNG-based animation

    I'd like to display a busy-animation in my program.

    KDE-Themes come with process-working-kde.png and process-working.png, which contain a number of pictures "below" each other (see attachment). What is the easiest way of telling to Qt to make an animation out of this? It must be used in KDE-apps, but I couldn't find the corresponding code…

    Thanks for your help!
    Attached Images Attached Images
    Quote Originally Posted by Bertolt Brecht
    What is the robbing of a bank compared to the founding of a bank?

  2. #2
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: PNG-based animation

    Header:
    Qt Code:
    1. #ifndef ANIM_H
    2. #define ANIM_H
    3. #include <QWidget>
    4. #include <QString>
    5. #include <QTimer>
    6. #include <QLabel>
    7. #include <QList>
    8. #include <QPixmap>
    9.  
    10. class AnimatedLabel : public QLabel
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. AnimatedLabel(const QString & image, const int imageCount, QWidget* parent = 0);
    16.  
    17. private slots:
    18. void changeImage();
    19.  
    20. private:
    21. QList<QPixmap> pixmaps;
    22. int currentPixmap;
    23. QTimer timer;
    24. };
    25.  
    26. #endif // ANIM_H
    To copy to clipboard, switch view to plain text mode 
    and source:
    Qt Code:
    1. #include "anim.h"
    2. #include <QApplication>
    3. #include <QImage>
    4.  
    5. AnimatedLabel::AnimatedLabel(const QString &image, const int imageCount, QWidget *parent)
    6. : QLabel(parent), currentPixmap(0)
    7. {
    8. QImage img;
    9. img.load(image);
    10. int subImageHeight = img.height() / imageCount;
    11.  
    12. for (int i = 0; i < imageCount; i++)
    13. {
    14. QImage subImage = img.copy(0, i * subImageHeight, img.width(), subImageHeight);
    15. pixmaps.push_back(QPixmap::fromImage(subImage));
    16. }
    17.  
    18. connect(&timer, SIGNAL(timeout()), SLOT(changeImage()));
    19. timer.start(100);
    20. changeImage();
    21. }
    22.  
    23. void AnimatedLabel::changeImage()
    24. {
    25. if (currentPixmap >= pixmaps.length())
    26. currentPixmap = 0;
    27.  
    28. setPixmap(pixmaps.at(currentPixmap));
    29.  
    30. currentPixmap++;
    31. }
    32.  
    33. int main(int argc, char *argv[])
    34. {
    35. QApplication a(argc, argv);
    36. AnimatedLabel bt("process-working.png", 8);
    37. bt.show();
    38. return a.exec();
    39. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to numbat for this useful post:

    soul_rebel (31st December 2009)

  4. #3
    Join Date
    Jan 2006
    Location
    germany
    Posts
    75
    Thanks
    9
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: PNG-based animation

    Awesome, thanks for the complete code!
    Quote Originally Posted by Bertolt Brecht
    What is the robbing of a bank compared to the founding of a bank?

Similar Threads

  1. Query about animation in QTabBar/QTabWidget
    By montylee in forum Qt Programming
    Replies: 6
    Last Post: 20th November 2009, 13:03
  2. Need a book or website that shows how to do qt animation
    By technoViking in forum Qt Programming
    Replies: 1
    Last Post: 6th November 2009, 05:00
  3. Some questions about QT animation?
    By tszzp in forum Qt Programming
    Replies: 7
    Last Post: 28th October 2009, 09:20
  4. Replies: 3
    Last Post: 28th August 2009, 13:45
  5. Animation and QGraphics
    By PrimeCP in forum Qt Programming
    Replies: 2
    Last Post: 7th May 2009, 00:31

Tags for this Thread

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.