gpt4 book ai didi

c++ - 如何在Qt5中的QToolButton上设置GIF图像

转载 作者:行者123 更新时间:2023-12-03 02:06:53 25 4
gpt4 key购买 nike

我想在QToolButton上显示“GIF”图像。

目前,我将 QMovie 设置为 QLable 并通过隐藏 QToolButton 显示 QLable

我也尝试使用样式表设置 gif,但失败了。

但是,是否可以直接将动画图像(.gif)显示到QToolButton

请指导我。

提前致谢。

最佳答案

#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();
}

关于c++ - 如何在Qt5中的QToolButton上设置GIF图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23133876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com