Skip to content

Instantly share code, notes, and snippets.

@buzzySmile
Created September 23, 2014 07:41
Show Gist options
  • Save buzzySmile/a493dfdef129d83ee35c to your computer and use it in GitHub Desktop.
Save buzzySmile/a493dfdef129d83ee35c to your computer and use it in GitHub Desktop.
This class supports QLabel blinking with fading effect
#include "QBlinkLabelControl.h"
QBlinkLabelControl::QBlinkLabelControl(QLabel *label, int _fade_time, int _fade_factor) :
m_Label(label),
m_iTime(_fade_time),
m_iFade(_fade_factor)
{
this->setMinimumWidth(70);
m_BackgroundColor = QColor(Qt::black);
QPalette pal = palette();
pal.setColor(backgroundRole(), m_BackgroundColor);
pal.setColor(foregroundRole(), Qt::yellow);
m_Label->setPalette(pal);
m_Label->setAutoFillBackground(true);
timer = new QTimer();
timer->setInterval(m_iTime);
connect(timer, SIGNAL(timeout()),
this, SLOT(fade_label()));
}
QBlinkLabelControl::~QBlinkLabelControl()
{
}
void QBlinkLabelControl::fade_label()
{
if ((m_Label->palette().color(backgroundRole()).value()) == 255 ) {
timer->stop();
return;
}
QColor fcolor = m_Label->palette().color(backgroundRole()).darker(m_iFade);
QPalette pal = palette();
pal.setColor(backgroundRole(), fcolor);
pal.setColor(foregroundRole(), Qt::yellow);
m_Label->setPalette(pal);
}
void QBlinkLabelControl::onChangeState(QBarGraphWidget::Colors _blinkColor)
{
timer->stop();
QPalette pal = palette();
pal.setColor(backgroundRole(), _blinkColor);
m_Label->setPalette(pal);
timer->start();
}
#include <QLabel>
#include <QTimer>
class QBlinkLabelControl: public QObject
{
Q_OBJECT
Q_ENUMS(Color)
public:
QBlinkLabelControl(QLabel *label, int _fade_time=5, int _fade_factor=101);
~QBlinkLabelControl();
private:
QColor m_BackgroundColor;
int m_iTime;
int m_iFade;
QLabel* m_Label;
QTimer *timer;
public slots:
void onChangeState(QColor _blinkColor);
private slots:
void fadeLabel();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment