如何给QPushButton按钮上添加一个gif动画?

边学边用边创造 2010-07-20 12:25:15
如何给QPushButton按钮上添加一个gif动画?
...全文
1042 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Aisy_D 2010-07-21
  • 打赏
  • 举报
回复


#include <QtGui>

class TOOTBtn : public QPushButton
{
Q_OBJECT
public:
TOOTBtn(const QString &imgPath, const QString &label, QWidget *parent = 0 )
: QPushButton(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"

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("zoe.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();
}


还有更高级的效果, 不便相告.....

  • 打赏
  • 举报
回复
[Quote=引用 4 楼 aisy_d 的回复:]

论坛里发言就"要" 的真不少啊.....
为有发扬QT,这个小demo,拿去吧.....


C/C++ code


#include <QtGui>

class TOOTBtn : public QPushButton
{
Q_OBJECT
public:
TOOTBtn(const QString &imgPath, const QString &……
[/Quote]


如果设置为按钮背景动画该怎么样做.?
mercedes2 2010-07-21
  • 打赏
  • 举报
回复
支持一下%……
Aisy_D 2010-07-20
  • 打赏
  • 举报
回复
用 QMoive 或者 paintevent + timer....
中才德创 2010-07-20
  • 打赏
  • 举报
回复
#include "main.moc"
包含moc文件,如此之怪?
Aisy_D 2010-07-20
  • 打赏
  • 举报
回复
论坛里发言就"要" 的真不少啊.....
为有发扬QT,这个小demo,拿去吧.....




#include <QtGui>

class TOOTBtn : public QPushButton
{
Q_OBJECT
public:
TOOTBtn(const QString &imgPath, const QString &label, QWidget *parent = 0 )
: QPushButton(label,parent)
{
if(!imgPath.isEmpty()){
_movie = new QMovie(imgPath, QByteArray(), this);
connect(_movie, SIGNAL(frameChanged(int)), this, SLOT(iconChged(int)));
_movie->start();
}

setIconSize(QSize(64,64));
}

private slots:
void iconChged(int){
setIcon( _movie->currentPixmap());
}

private:
QMovie *_movie;

};


#include "main.moc"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QWidget window;
TOOTBtn btn("zoe.gif", "Test Btn....", &window);
btn.move(100,100);
window.show( );
window.resize(640,400);
window.setWindowTitle("TOOTzoe.com Animating Button Test....");
QObject::connect(&btn, SIGNAL(clicked()), &window, SLOT(close()));

return app.exec();
}
tzcherish 2010-07-20
  • 打赏
  • 举报
回复

路过的~
tingsking18 2010-07-20
  • 打赏
  • 举报
回复
gif需要plugin\imageformats\qgif4.dll

16,815

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧