我想在"GIF"
上显示QToolButton
图片。
目前,我将QMovie
设为QLable
并隐藏QLable
以显示QToolButton
。
我还尝试使用样式表设置gif
,但失败了。
但是,是否可以将动画图像(.gif)直接显示到QToolButton
?
请指导我。
提前致谢。
答案 0 :(得分: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();
}
答案 1 :(得分:-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);
该代码取自正在运行的应用程序Qt4。用按钮的父级创建标签,这是美丽的Qt。