简单的带动画渐隐效果的 TPushButton....

Aisy_D 2010-09-23 09:26:12


点击下载.....

直接贴代码:
   

///tmainwnd.h

#ifndef TMAINWND_H
#define TMAINWND_H


#include <QWidget>
#include <QPushButton>

class TPushBtn2 : public QPushButton
{
Q_OBJECT
public:
explicit TPushBtn2(QWidget *parent = 0);
TPushBtn2(const QString &,QWidget *parent = 0);
void UpdateAlpha();

signals:

public slots:
protected:
void timerEvent(QTimerEvent *e);
void enterEvent(QEvent *);
void leaveEvent(QEvent *);

void changeMe();

private:
int _alpha;
int _alphaTimer;
qint8 _nDirection;

};

class TMainWnd : public QWidget
{
Q_OBJECT
public:
explicit TMainWnd(QWidget *parent = 0);

signals:

public slots:

};

#endif // TMAINWND_H






///tmainwnd.cpp


#include <QVBoxLayout>
#include <QTimerEvent>


#include "tmainwnd.h"

TMainWnd::TMainWnd(QWidget *parent) :
QWidget(parent)
{
QVBoxLayout *mainLO = new QVBoxLayout;

for(int i = 0 ; i < 10 ; i ++){
TPushBtn2 *btn = new TPushBtn2(tr("Test %1").arg(i));
mainLO->addWidget(btn );
}

// style()->drawControl(.drawPushButton(this, paint);

setLayout(mainLO);

}


TPushBtn2::TPushBtn2(QWidget *parent) :
QPushButton(parent)
{}

TPushBtn2::TPushBtn2(const QString &text, QWidget *parent)
:QPushButton(text,parent),_nDirection(1)
{}

void TPushBtn2::timerEvent(QTimerEvent *e)
{
changeMe();
}

void TPushBtn2::enterEvent(QEvent *)
{
_alpha = 255;
_nDirection = 1;
changeMe();
}

void TPushBtn2::leaveEvent(QEvent *)
{
if(_alphaTimer) killTimer(_alphaTimer);
_nDirection = -1;
_alphaTimer = startTimer(100);
}

void TPushBtn2::changeMe()
{
_alpha += 45 * _nDirection;

QPalette pal =this-> palette();

QColor cr = pal.window().color();

if(_alpha >= 255){
if(_alphaTimer) killTimer(_alphaTimer);
_alphaTimer = 0;
_alpha = 255 ;
} else if( _alpha <= 0) {
killTimer(_alphaTimer);
_alphaTimer = 0;
_alpha = 0 ;
}
cr.setRed(_alpha);
cr.setBlue(_alpha);
pal.setColor(QPalette::All,QPalette::Button , cr);

this-> setPalette(pal);

}



////main.cpp
#include <QApplication>

#include "tmainwnd.h"


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

TMainWnd window;
window.show();
window.resize(800,500);


return app.exec();
}


这只是一个粗糙的例子,我只用来测试目的....当然,想要做出真正高光渐隐的效果,例如 QQ 聊天窗口在上面那些工具按钮的高光渐隐效果,有QT 框架的支持 也不是很难的.....只要在 QStyle 里面做的手脚就可以实现了.....
...全文
962 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
Schuman 2011-02-27
  • 打赏
  • 举报
回复
请问窗口的半透明是怎么做的?
g_tree2003 2011-01-03
  • 打赏
  • 举报
回复
我win7 64被假死了
bbnjack 2010-09-29
  • 打赏
  • 举报
回复
好帖,顶啦!
Aisy_D 2010-09-28
  • 打赏
  • 举报
回复
加个了 statusbar, 让窗口可放缩....下载运行可见效果....
编程夜猫 2010-09-27
  • 打赏
  • 举报
回复
不错。最近我也在学Style的东西,学习了。我是用stylesheet样式的方式,不知楼主对窗口的如何做成QQ的样式。如窗口带圆角、标题栏的圆润效果等。。
yuyin86 2010-09-27
  • 打赏
  • 举报
回复
实现界面更新啊!挺好啊
mercedes2 2010-09-26
  • 打赏
  • 举报
回复
一片滑过的时候感觉还不错……
enum_623 2010-09-25
  • 打赏
  • 举报
回复
恩,不错!
Aisy_D 2010-09-25
  • 打赏
  • 举报
回复
更新了一下界面.....
lefttime 2010-09-24
  • 打赏
  • 举报
回复
嗯, 效果不错~``

16,212

社区成员

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

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