本文介绍了如何动画图标设置为QPushButton在QT5?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

QPushButton 可以有图标,但我需要动画图标设置它。如何做到这一点?
我创建从 QPushButton 实施了新的类,但如何从 QIcon则更换图标 QMovie

QPushButton can have icon, but I need to set animated icon to it. How to do this?I created new class implemented from QPushButton but how to replace icon from QIcon to QMovie?

推荐答案

这可以在不通过简单地使用Qt的信号/槽机制继承 QPushButton 来完成。的 QMovie frameChanged 信号连接到在类包含此 QPushButton 。该功能将适用 QMovie 的当前帧的 QPushButton 图标。它应该是这个样子:

This can be accomplished without subclassing QPushButton by simply using the signal / slot mechanism of Qt. Connect the frameChanged signal of QMovie to a custom slot in the class that contains this QPushButton. This function will apply the current frame of the QMovie as the icon of the QPushButton. It should look something like this:

// member function that catches the frameChanged signal of the QMovie
void MyWidget::setButtonIcon(int frame)
{
    myPushButton->setIcon(QIcon(myMovie->currentPixmap()));
}

和分配你的 QMovie QPushButton 成员......当

And when allocating your QMovie and QPushButton members ...

myPushButton = new QPushButton();
myMovie = new QMovie("someAnimation.gif");

connect(myMovie,SIGNAL(frameChanged(int)),this,SLOT(setButtonIcon(int)));

// if movie doesn't loop forever, force it to.
if (myMovie->loopCount() != -1)
    connect(myMovie,SIGNAL(finished()),myMovie,SLOT(start()));

myMovie->start();

这篇关于如何动画图标设置为QPushButton在QT5?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 15:55