Displaying Animated GIFs as Objects in Qt Main Window Using QGraphicsView

Overview

This approach enables embedding multiple animated GIFs as individual objects within a single window. Each GIF becomes a separate entity that can potentially suport its own functionality.

Core Componnets

The implementation requires three main classes:

  • QGraphicsView: Provides the viewport widget for displaying graphics
  • QGraphicsScene: Manages the collection of graphical items
  • QGraphicsItem: Base class for custom graphics objects

Header Files

#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsItem>

Implementation Steps

Step 1: Creating the Graphics View and Scene

In the main window implementation:

// Create graphics view and attach to main window
QGraphicsView* graphicsView = new QGraphicsView(this);
graphicsView->setGeometry(0, 0, windowWidth, windowHeight);
graphicsView->show();

// Create scene with specified dimensions
QRect sceneRect(0, 0, windowWidth, windowHeight);
QGraphicsScene* scene = new QGraphicsScene(sceneRect);

// Configure view appearance
graphicsView->setStyleSheet("background: transparent; border: 0px");
graphicsView->setScene(scene);

Replace windowWidth and windowHeight with actual dimension values.

Step 2: Creating Custom Graphics Item Class

The custom class must inherit from both QLabel and QGraphicsItem:

class AnimatedGifItem : public QLabel, public QGraphicsItem
{
    Q_OBJECT
    Q_INTERFACES(QGraphicsItem)

public:
    AnimatedGifItem(int posX, int posY, const QString& gifPath);
    void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, 
              QWidget* widget = Q_NULLPTR) Q_DECL_OVERRIDE;
    QRectF boundingRect() const Q_DECL_OVERRIDE;

private:
    int positionX;
    int positionY;
    QString filePath;
};

Required override functions:

void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, 
          QWidget* widget = Q_NULLPTR) Q_DECL_OVERRIDE;

QRectF boundingRect() const Q_DECL_OVERRIDE;

Step 3: Adding Items to Scene

scene->addWidget(animatedItem);
scene->addItem(animatedItem);

Complete Code Example

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char* argv[])
{
    QApplication app(argc, argv);
    MainWindow window;
    window.resize(400, 300);
    window.show();
    return app.exec();
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QGraphicsView>
#include <QGraphicsScene>


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget* parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow* ui;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "animatedgifitem.h"

#include <QGraphicsView>
#include <QGraphicsScene>
#include <QRect>


MainWindow::MainWindow(QWidget* parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // Initialize graphics view
    QGraphicsView* graphicsView = new QGraphicsView(this);
    graphicsView->setGeometry(0, 0, 400, 300);
    graphicsView->show();

    // Create scene
    QRect sceneRect(0, 0, 400, 300);
    QGraphicsScene* scene = new QGraphicsScene(sceneRect);

    // Configure view
    graphicsView->setStyleSheet("background: transparent; border: 0px");
    graphicsView->setScene(scene);

    // Create animated GIF item
    AnimatedGifItem* gifItem = new AnimatedGifItem(
        50, 50, 
        "C:/path/to/animation.gif"
    );

    // Add to scene
    scene->addWidget(gifItem);
    scene->addItem(gifItem);
}

MainWindow::~MainWindow()
{
    delete ui;
}

animatedgifitem.h

#ifndef ANIMATEDGIFITEM_H
#define ANIMATEDGIFITEM_H

#include <QLabel>
#include <QGraphicsItem>
#include <QString>
#include <QRectF>
#include <QMovie>


class AnimatedGifItem : public QLabel, public QGraphicsItem
{
    Q_OBJECT
    Q_INTERFACES(QGraphicsItem)


public:
    AnimatedGifItem(int posX, int posY, const QString& gifPath);
    void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, 
              QWidget* widget = Q_NULLPTR) Q_DECL_OVERRIDE;
    QRectF boundingRect() const Q_DECL_OVERRIDE;

private:
    int positionX;
    int positionY;
    QString filePath;
};

#endif // ANIMATEDGIFITEM_H

animatedgifitem.cpp

#include "animatedgifitem.h"

AnimatedGifItem::AnimatedGifItem(int posX, int posY, const QString& gifPath)
    : positionX(posX), positionY(posY), filePath(gifPath)
{
    // Create movie from file
    QMovie* movie = new QMovie(filePath);
    setMovie(movie);
    movie->start();

    // Configure appearance
    setScaledContents(true);
    setGeometry(posX, posY, 150, 150);
    setAttribute(Qt::WA_TranslucentBackground);
    show();
}

void AnimatedGifItem::paint(QPainter* painter, 
                            const QStyleOptionGraphicsItem* option, 
                            QWidget* widget)
{
    // Implementation handled by QLabel
    Q_UNUSED(painter);
    Q_UNUSED(option);
    Q_UNUSED(widget);
}

QRectF AnimatedGifItem::boundingRect() const
{
    return QRectF(positionX, positionY, 150, 150);
}

Key Notes

  • Multiple animated items can be added to the same scene
  • Each item maintains its own animation state independently
  • The boundingRect() function must return accurate dimensions for proper hit testing
  • The Q_OBJECT macro is required for any class using signals and slots

Tags: Qt QGraphicsView QGraphicsScene GIF animation

Posted on Wed, 05 Aug 2026 16:16:43 +0000 by simflex