Skip to content

Instantly share code, notes, and snippets.

@jniemann66
Created April 7, 2016 05:05
Show Gist options
  • Save jniemann66/53e7edab0ed39310820f8930a202f968 to your computer and use it in GitHub Desktop.
Save jniemann66/53e7edab0ed39310820f8930a202f968 to your computer and use it in GitHub Desktop.
Making a flashing button in Qt
#ifndef FLASHINGPUSHBUTTON_H
#define FLASHINGPUSHBUTTON_H
#include <QPushButton>
#include <QTimer>
// class flashingPushButton
// Description: a QPushButton which flashes on and off whenever it is Disabled (to indicate that the app is busy processing ...)
class flashingPushbutton : public QPushButton{
public:
flashingPushbutton(QWidget* parent=0) : QPushButton(parent), flashState(0)
{
connect(&timer, &QTimer::timeout,this, &flashingPushbutton::flashWhenDisabled);
timer.start(500);
}
~flashingPushbutton()
{
}
private slots:
void flashWhenDisabled()
{
if (!this->isEnabled()) // flash when disabled
{
if (flashState == 0){
flashState = 1;
this->setProperty("flashing", true);
}
else if (flashState == 1){
flashState = 0;
this->setProperty("flashing", false);
}
style()->unpolish(this);
style()->polish(this);
}
else { // don't flash
if (flashState != 0) {
flashState = 0;
this->setProperty("flashing", false);
style()->unpolish(this);
style()->polish(this);
}
}
}
private:
int flashState;
QTimer timer;
};
#endif // FLASHINGPUSHBUTTON_H
QPushButton {
color: #fff;
border: 1px solid #1b2018;
border-radius: 9px;
padding: 5px;
background: qradialgradient(cx: 0.3, cy: -0.4,
fx: 0.3, fy: -0.4,
radius: 1.35, stop: 0 #46503f, stop: 1 #2d3328);
min-width: 66px;
}
QPushButton:hover {
background: qradialgradient(cx: 0.3, cy: -0.4,
fx: 0.3, fy: -0.4,
radius: 1.35, stop: 0 #535e4a, stop: 1 #3a4234);
}
QPushButton:pressed {
background: qradialgradient(cx: 0.4, cy: -0.1,
fx: 0.4, fy: -0.1,
radius: 1.35, stop: 0 #70e01a, stop: 1#3b770e);
}
QPushButton:disabled {
background: qradialgradient(cx: 0.4, cy: -0.1,
fx: 0.4, fy: -0.1,
radius: 1.35, stop: 0 #70e01a, stop: 1#3b770e);
}
QPushButton:disabled[flashing="true"] {
background: qradialgradient(cx: 0.4, cy: -0.1,
fx: 0.4, fy: -0.1,
radius: 1.35, stop: 0 #3a4234, stop: 1#21251d);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment