Qt之对话框设计——淡入淡出效果

实例化一个QPainter类的窗体对象。首先设置该窗体显示的最初透明度为255,即不透明;启动定时器后,以一定的周期重画该窗体并使窗体的透明度递减,直至透明度为0,停止定时器,关闭窗体。


fadewidget.h

  1. #ifndef FADEWIDGET_H  
  2. #define FADEWIDGET_H  
  3.   
  4. #include <QWidget>  
  5.   
  6. class QColor;  
  7. class QTimer;  
  8.   
  9. class FaderWidget : public QWidget  
  10. {  
  11.     Q_OBJECT  
  12.   
  13. public:  
  14.     FaderWidget(QWidget *parent);  
  15.       
  16.     void start();  
  17.       
  18. protected:  
  19.     void paintEvent(QPaintEvent *event);  
  20.       
  21. private:  
  22.   
  23.     QColor startColor;  
  24.     int currentAlpha;  
  25.     int fadeTimes;  
  26.     QTimer *timer;  
  27. };  
  28.   
  29. #endif  // FADER_H  

fadewidget.cpp

  1. #include "fadewidget.h"  
  2. #include <QtGui>  
  3.   
  4. FaderWidget::FaderWidget(QWidget *parent)  
  5.         : QWidget(parent)  
  6. {  
  7.     if (parent)  
  8.         startColor = parent->palette().window().color();  
  9.     else  
  10.         startColor = Qt::white;  
  11.       
  12.     currentAlpha = 0;  
  13.     fadeTimes = 1000;  
  14.       
  15.     timer = new QTimer(this);  
  16.     connect(timer, SIGNAL(timeout()),this, SLOT(update()));  
  17.       
  18.     setAttribute(Qt::WA_DeleteOnClose);  
  19.     resize(parent->size());  
  20. }  
  21.   
  22.   
  23. void FaderWidget::start()  
  24. {  
  25.     currentAlpha = 255;  
  26.     timer->start(100);  
  27.     show();  
  28. }  
  29.   
  30. void FaderWidget::paintEvent(QPaintEvent * /* event */)  
  31. {  
  32.     QPainter painter(this);  
  33.     QColor currentColor = startColor;  
  34.     currentColor.setAlpha(currentAlpha);  
  35.     painter.fillRect(rect(), currentColor);  
  36.       
  37.     currentAlpha -= 255 * timer->interval() / fadeTimes;  
  38.     if (currentAlpha <= 0)   
  39.     {  
  40.         timer->stop();  
  41.         close();  
  42.     }  
  43. }  

作者: 韩兆新
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
分类:  [02]Qt语言基础
标签:  Qt学习笔记

本文转自韩兆新博客博客园博客,原文链接:http://www.cnblogs.com/hanzhaoxin/archive/2012/11/22/2783222.html,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值