Qt实用技巧:会呼吸的痛(呼吸点/呼吸灯)
来源:互联网 发布:果冻手表淘宝 编辑:程序博客网 时间:2024/04/24 02:03
Demo下载地址:http://download.csdn.net/download/qq21497936/10150349
入坑
主窗口为QWidget的子类时,不论设置QPallet和setStyleSheets设置背景图片是无法生效的,但设置颜色却可以生效。
Demo目录结构
关键代码
已经在主窗口上添加了一个QWidget
主窗口关键代码,设置布局,生成6个呼吸点
- frmLightPoint::frmLightPoint(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::frmLightPoint)
- {
- ui->setupUi(this);
-
-
- ui->widget->setStyleSheet("QWidget#widget {background: url(:/image/bg1.jpg); }");
-
- QGridLayout * pLayout = new QGridLayout();
-
- LightPoint *pLightPoint;
- for(int index = 0; index < 6; index++)
- {
- pLightPoint = new LightPoint(this);
- pLayout->addWidget(pLightPoint, index/3, index%3, 1, 1);
-
- widgets << pLightPoint;
- }
-
- ui->widget->setLayout(pLayout);
-
- QList<QColor> colors;
- colors << "#47A4E9" << "#00B17D" << "#D64D54" << "#DEAF39" << "#A279C5" << "#009679";
-
- for (int index = 0; index < widgets.count(); index++) {
- widgets.at(index)->setBgColor(colors.at(index));
- }
- }
呼吸点控件头文件 lightpoint.h
- #ifndef LIGHTPOINT_H
- #define LIGHTPOINT_H
-
- #include <QWidget>
-
- #ifdef quc
- #if (QT_VERSION < QT_VERSION_CHECK(5,7,0))
- #include <QtDesigner/QDesignerExportWidget>
- #else
- #include <QtUiPlugin/QDesignerExportWidget>
- #endif
-
- class QDESIGNER_WIDGET_EXPORT LightPoint : public QWidget
- #else
- class LightPoint : public QWidget
- #endif
- {
- Q_OBJECT
- Q_PROPERTY(int step READ getStep WRITE setStep)
- Q_PROPERTY(int interval READ getInterval WRITE setInterval)
- Q_PROPERTY(QColor bgColor READ getBgColor WRITE setBgColor)
-
- public:
- explicit LightPoint(QWidget *parent = 0);
- ~LightPoint();
-
- protected:
- void paintEvent(QPaintEvent *) override;
- void drawBg(QPainter *painter);
-
- private:
- int step;
- int interval;
- QColor bgColor;
-
- QTimer *timer;
- int offset;
- bool add;
-
- public:
- int getStep() const;
- int getInterval() const;
- QColor getBgColor() const;
-
- QSize sizeHint() const;
- QSize minimumSizeHint() const;
-
- public slots:
-
- void setStep(int step);
-
-
- void setInterval(int interval);
-
-
- void setBgColor(const QColor &bgColor);
-
- };
-
- #endif // LIGHTPOINT_H
呼吸点控件源代码 lightpoint.cpp
- #include "lightpoint.h"
- #include "qpainter.h"
- #include "qevent.h"
- #include "qtimer.h"
-
- LightPoint::LightPoint(QWidget *parent) : QWidget(parent)
- {
- step = 10;
- interval = 100;
- bgColor = QColor(255, 0, 0);
-
- timer = new QTimer(this);
- connect(timer, SIGNAL(timeout()), this, SLOT(update()));
- timer->start(100);
-
- offset = 0;
- add = true;
- }
-
- LightPoint::~LightPoint()
- {
- if (timer->isActive()) {
- timer->stop();
- }
- }
-
- void LightPoint::paintEvent(QPaintEvent *)
- {
- QPainter painter(this);
-
- painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
-
- painter.translate(rect().width() / 2, rect().height() / 2);
-
- drawBg(&painter);
- }
-
- void LightPoint::drawBg(QPainter *painter)
- {
-
- int radius = qMin(rect().width(), rect().height())/2;
-
- painter->save();
-
- QRadialGradient g(QPoint(0, 0), radius);
-
- (offset < 100 && add) ? (offset += step) : (add = false);
- (offset > 0 && !add) ? (offset -= step) : (add = true);
-
- bgColor.setAlpha( 200+offset > 255 ? 255 : 200+offset );
- g.setColorAt(0.0, bgColor);
- bgColor.setAlpha( 140+offset);
- g.setColorAt(0.2, bgColor);
- bgColor.setAlpha( 80+offset);
- g.setColorAt(0.4, bgColor);
- bgColor.setAlpha( 20+offset >= 0 ? 20+offset : 0 );
- g.setColorAt(0.6, bgColor);
- bgColor.setAlpha( -60+offset >= 0 ? -50+offset : 0 );
- g.setColorAt(0.8, bgColor);
- bgColor.setAlpha( 0 );
- g.setColorAt(1.0, bgColor);
-
- painter->setPen(Qt::NoPen);
-
- painter->setBrush(g);
-
- painter->drawEllipse(-radius, -radius, radius * 2, radius * 2);
-
- painter->restore();
- }
-
- int LightPoint::getStep() const
- {
- return this->step;
- }
-
- int LightPoint::getInterval() const
- {
- return this->interval;
- }
-
- QColor LightPoint::getBgColor() const
- {
- return this->bgColor;
- }
-
- QSize LightPoint::sizeHint() const
- {
- return QSize(100, 100);
- }
-
- QSize LightPoint::minimumSizeHint() const
- {
- return QSize(5, 5);
- }
-
- void LightPoint::setStep(int step)
- {
- if (this->step != step) {
- this->step = step;
- update();
- }
- }
-
- void LightPoint::setInterval(int interval)
- {
- if (this->interval != interval) {
- this->interval = interval;
- timer->setInterval(interval);
- update();
- }
- }
-
- void LightPoint::setBgColor(const QColor &bgColor)
- {
- if (this->bgColor != bgColor) {
- this->bgColor = bgColor;
- update();
- }
- }