What is the best way to display an animated icon in a QTableView?

后端 未结 5 1432
生来不讨喜
生来不讨喜 2021-02-05 19:54

I\'ve been struggling with this for some times now, and I can\'t seem to find the right way to do this.

What I would like is the ability to use an animated icon as a dec

5条回答
  •  无人及你
    2021-02-05 20:14

    One solution is to use QMovie with GIF. I also tried using SVG (it's lightweight and offers support for transparency), but both QMovie and QImageReader don't seem to support animated SVG.

    Model::Model(QObject* parent) : QFileSystemModel(parent)
    {
        movie = new QMovie{ ":/resources/img/loading.gif" };
        movie->setCacheMode(QMovie::CacheAll);
        movie->start();
    
        connect(movie, &QMovie::frameChanged,
        [this] {
            dataChanged(index(0, 0), index(rowCount(), 0),
                QVector{QFileSystemModel::FileIconRole});
        });
    }
    
    QVariant Model::data(const QModelIndex& index, int role) const
    {
        case QFileSystemModel::FileIconRole:
        {
            if (index.column() == 0) {
                auto const path = QString{ index.data(QFileSystemModel::FilePathRole).toString() };
    
                if (path.isBeingLoaded()){
                    return movie->currentImage();
                }
            }
        }
    }
    

提交回复
热议问题