我想在QToolButton
上显示"GIF"
图像。
当前,我正在将QMovie
设置为QLable
,并通过隐藏QToolButton
来显示QLable
。
我也尝试过使用样式表设置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。用按钮的父项创建一个标签,这就是美丽的Qt。