Qt实用技巧:会呼吸的痛(呼吸点/呼吸灯)

来源:互联网 发布:果冻手表淘宝 编辑:程序博客网 时间:2024/04/24 02:03

  

        Demo下载地址:http://download.csdn.net/download/qq21497936/10150349

入坑

        主窗口为QWidget的子类时,不论设置QPallet和setStyleSheets设置背景图片是无法生效的,但设置颜色却可以生效。

Demo目录结构

        

关键代码

已经在主窗口上添加了一个QWidget

主窗口关键代码,设置布局,生成6个呼吸点

[cpp]view plaincopy
  1. frmLightPoint::frmLightPoint(QWidget *parent) :  
  2.     QWidget(parent),  
  3.     ui(new Ui::frmLightPoint)  
  4. {  
  5.     ui->setupUi(this);  
  6.   
  7.     // 设置背景图片  
  8.     ui->widget->setStyleSheet("QWidget#widget {background: url(:/image/bg1.jpg); }");  
  9.     // 使用Grid布局  
  10.     QGridLayout * pLayout = new QGridLayout();  
  11.     // 生成 2行 3列 共 6 个控件  
  12.     LightPoint *pLightPoint;  
  13.     for(int index = 0; index < 6; index++)  
  14.     {  
  15.         pLightPoint = new LightPoint(this);  
  16.         pLayout->addWidget(pLightPoint, index/3, index%3, 1, 1);  
  17.         // 控件指针加入列表,以便设置颜色,等同于 widgets.push_back(pLightPoint);  
  18.         widgets << pLightPoint;  
  19.     }  
  20.     // 设置布局到主窗口上的QWidget  
  21.     ui->widget->setLayout(pLayout);  
  22.     // 颜色列表  
  23.     QList<QColor> colors;  
  24.     colors << "#47A4E9" << "#00B17D" << "#D64D54" << "#DEAF39" << "#A279C5" << "#009679";  
  25.     // 循环设置颜色  
  26.     for (int index = 0; index < widgets.count(); index++) {  
  27.         widgets.at(index)->setBgColor(colors.at(index));  
  28.     }  
  29. }  


呼吸点控件头文件 lightpoint.h

[cpp]view plaincopy
  1. #ifndef LIGHTPOINT_H  
  2. #define LIGHTPOINT_H  
  3.   
  4. #include <QWidget>  
  5.   
  6. #ifdef quc  
  7. #if (QT_VERSION < QT_VERSION_CHECK(5,7,0))  
  8. #include <QtDesigner/QDesignerExportWidget>  
  9. #else  
  10. #include <QtUiPlugin/QDesignerExportWidget>  
  11. #endif  
  12.   
  13. class QDESIGNER_WIDGET_EXPORT LightPoint : public QWidget  
  14. #else  
  15. class LightPoint : public QWidget  
  16. #endif  
  17. {  
  18.     Q_OBJECT  
  19.     Q_PROPERTY(int step READ getStep WRITE setStep)  
  20.     Q_PROPERTY(int interval READ getInterval WRITE setInterval)      
  21.     Q_PROPERTY(QColor bgColor READ getBgColor WRITE setBgColor)  
  22.   
  23. public:  
  24.     explicit LightPoint(QWidget *parent = 0);  
  25.     ~LightPoint();  
  26.   
  27. protected:  
  28.     void paintEvent(QPaintEvent *) override;  
  29.     void drawBg(QPainter *painter);  
  30.   
  31. private:  
  32.     int step;                       //颜色透明渐变步长  
  33.     int interval;                   //定时器间隔      
  34.     QColor bgColor;                 //背景颜色  
  35.   
  36.     QTimer *timer;                  //绘制定时器  
  37.     int offset;                     //偏移量  
  38.     bool add;                       //是否增加  
  39.   
  40. public:  
  41.     int getStep()                   const;  
  42.     int getInterval()               const;      
  43.     QColor getBgColor()             const;  
  44.   
  45.     QSize sizeHint()                const;  
  46.     QSize minimumSizeHint()         const;  
  47.   
  48. public slots:  
  49.     //设置颜色透明渐变步长  
  50.     void setStep(int step);  
  51.   
  52.     //设置定时器间隔  
  53.     void setInterval(int interval);    
  54.   
  55.     //设置背景颜色  
  56.     void setBgColor(const QColor &bgColor);  
  57.   
  58. };  
  59.   
  60. #endif // LIGHTPOINT_H  


呼吸点控件源代码 lightpoint.cpp

[cpp]view plaincopy
  1. #include "lightpoint.h"  
  2. #include "qpainter.h"  
  3. #include "qevent.h"  
  4. #include "qtimer.h"  
  5.   
  6. LightPoint::LightPoint(QWidget *parent) : QWidget(parent)  
  7. {  
  8.     step = 10;  
  9.     interval = 100;      
  10.     bgColor = QColor(255, 0, 0);  
  11.   
  12.     timer = new QTimer(this);  
  13.     connect(timer, SIGNAL(timeout()), this, SLOT(update()));  
  14.     timer->start(100);  
  15.   
  16.     offset = 0;  
  17.     add = true;  
  18. }  
  19.   
  20. LightPoint::~LightPoint()  
  21. {  
  22.     if (timer->isActive()) {  
  23.         timer->stop();  
  24.     }  
  25. }  
  26.   
  27. void LightPoint::paintEvent(QPaintEvent *)  
  28. {  
  29.     QPainter painter(this);  
  30.     // 绘制准备工作 启用反锯齿  
  31.     painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);  
  32.     // 平移坐标轴中心,  
  33.     painter.translate(rect().width() / 2, rect().height() / 2);  
  34.     //绘制背景  
  35.     drawBg(&painter);  
  36. }  
  37.   
  38. void LightPoint::drawBg(QPainter *painter)  
  39. {  
  40.     // 半径为当前 宽 或者 高 的一半  
  41.     int radius = qMin(rect().width(), rect().height())/2;  
  42.     // 保存当前painter  
  43.     painter->save();  
  44.     // 以点为中心的渐变色  
  45.     QRadialGradient g(QPoint(0, 0), radius);  
  46.     // 循环加减  
  47.     (offset < 100 && add) ? (offset += step) : (add = false);  
  48.     (offset > 0 && !add) ? (offset -= step) : (add = true);  
  49.     // 按照 点范围[0.0,1.0] 对于 每点的颜色  
  50.     bgColor.setAlpha( 200+offset > 255 ? 255 : 200+offset );  
  51.     g.setColorAt(0.0, bgColor);  
  52.     bgColor.setAlpha( 140+offset);  
  53.     g.setColorAt(0.2, bgColor);  
  54.     bgColor.setAlpha( 80+offset);  
  55.     g.setColorAt(0.4, bgColor);  
  56.     bgColor.setAlpha( 20+offset >= 0 ? 20+offset : 0 );  
  57.     g.setColorAt(0.6, bgColor);  
  58.     bgColor.setAlpha( -60+offset >= 0 ? -50+offset : 0 );  
  59.     g.setColorAt(0.8, bgColor);  
  60.     bgColor.setAlpha( 0 );  
  61.     g.setColorAt(1.0, bgColor);  
  62.     // 设置 画笔 图形的边界线  
  63.     painter->setPen(Qt::NoPen);  
  64.     // 设置 画刷 画刷为 点向外辐射的渐变色  
  65.     painter->setBrush(g);  
  66.     // 画椭圆,长=宽 为原型  
  67.     painter->drawEllipse(-radius, -radius, radius * 2, radius * 2);  
  68.     // 回复保存的  
  69.     painter->restore();  
  70. }  
  71.   
  72. int LightPoint::getStep() const  
  73. {  
  74.     return this->step;  
  75. }  
  76.   
  77. int LightPoint::getInterval() const  
  78. {  
  79.     return this->interval;  
  80. }  
  81.   
  82. QColor LightPoint::getBgColor() const  
  83. {  
  84.     return this->bgColor;  
  85. }  
  86.   
  87. QSize LightPoint::sizeHint() const  
  88. {  
  89.     return QSize(100, 100);  
  90. }  
  91.   
  92. QSize LightPoint::minimumSizeHint() const  
  93. {  
  94.     return QSize(5, 5);  
  95. }  
  96.   
  97. void LightPoint::setStep(int step)  
  98. {  
  99.     if (this->step != step) {  
  100.         this->step = step;  
  101.         update();  
  102.     }  
  103. }  
  104.   
  105. void LightPoint::setInterval(int interval)  
  106. {  
  107.     if (this->interval != interval) {  
  108.         this->interval = interval;  
  109.         timer->setInterval(interval);  
  110.         update();  
  111.     }  
  112. }  
  113.   
  114. void LightPoint::setBgColor(const QColor &bgColor)  
  115. {  
  116.     if (this->bgColor != bgColor) {  
  117.         this->bgColor = bgColor;  
  118.         update();  
  119.     }  
  120. }  
原创粉丝点击