4

I want to display "GIF" image on QToolButton.

Currently, I am setting QMovie to QLable and display QLable by hiding QToolButton.

I have also try to set gif using stylesheet, but fails.

But, Is it possible to display animated image (.gif) directly to QToolButton ?

Please guide me.

Thanks in advance.

AB Bolim
  • 1,997
  • 2
  • 23
  • 47
  • Not submitting an answer because I did not check if this would work: maybe you can do setIcon(myMovie) on QToolButton. This might be helpful: https://stackoverflow.com/questions/15374191/how-to-set-animated-icon-to-qpushbutton-in-qt5 – goozez Nov 14 '19 at 20:42

2 Answers2

0
#include <QtGui>
#include <QToolButton>

class TOOTBtn : public QToolButton
{
    Q_OBJECT
public:
    TOOTBtn(const QString &imgPath, const QString &label, QWidget *parent = 0)
        : QToolButton(parent), _label(label)
    {
        if (!imgPath.isEmpty()) {
            _movie = new QMovie(imgPath, QByteArray(), this);
            connect(_movie, SIGNAL(frameChanged(int)), this, SLOT(iconChged(int)));
            _movie->start();
        }

    }

    private slots:
    void iconChged(int) {

        QTextDocument Text;
        Text.setHtml(_label);

        QPixmap currFrame = _movie->currentPixmap();
        QPixmap pixmap(Text.size().width(), currFrame.height());
        pixmap.fill(Qt::transparent);
        QPainter painter(&pixmap);
        painter.drawPixmap((pixmap.width() - currFrame.width()) / 2,
            (pixmap.height() - currFrame.height()) / 2, currFrame);
        Text.drawContents(&painter, pixmap.rect());

        setIcon(QIcon(pixmap));
        setIconSize(pixmap.rect().size());
    }

private:
    QMovie *_movie;
    QString _label;

};


#include "main.moc"
#include <QApplication>
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QWidget window;
    QString TOOTLabel = QObject::tr("<h2><i>Hello</i> <font color=red>TOOTzoe.com</font></h2>"
        "<b>Animating Button Test....<b>");
    TOOTBtn btn("a.gif", TOOTLabel, &window);
    btn.move(100, 100);
    window.show();
    window.resize(640, 400);
    window.setWindowTitle("Animating Button Test....");
    QObject::connect(&btn, SIGNAL(clicked()), &window, SLOT(close()));

    return app.exec();
}
Ye.Feng
  • 709
  • 6
  • 9
-1
QMovie*fan_gif=new QMovie("://png/gif/fan.gif");
QSize MovSize(48, 48);
fan_gif->setScaledSize(MovSize);
QLabel*pLabel = new QLabel(ui->toolButton_fan_1);
pLabel->setMovie(fan_gif);
fan_gif->start();
pLabel->setGeometry(5, 5, 48, 48);

The code is taken from the working application Qt4. Create a label with the parent of the button, this is the beauty Qt.

okiest
  • 1
  • 1
  • 1
    Are you sure that code shows a gif in the QToolButton? – eyllanesc Nov 14 '19 at 18:57
  • 1
    Please, elaborate on your answer, why and how exactly this should help to solve the original problem? Code-only answers are confusing. – VP. Nov 15 '19 at 12:24
  • Place the qtoolbutton in the qdialog. The size of the button 58x58. In the constructor, create a qlabel with a parent from the qtoolbutton placed in the qdialog. – okiest Nov 22 '19 at 19:23