我想在
"GIF"
上显示 QToolButton
图像。
目前,我将
QMovie
设置为 QLabel
,并通过隐藏 QLabel
显示 QToolButton
。
我也尝试过使用样式表设置 GIF,但失败了。
是否可以直接在
QToolButton
上显示动画图像(.gif)?
#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();
}
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);
代码取自工作应用程序 Qt4。创建一个以按钮为父级的标签。