PDA

View Full Version : Blinking Buttons



metdos
22nd July 2009, 07:18
I need a blinking button between grey and red with 1 sec breaks. I tried to do this with sleep() function in c++, but it did not work properly, and whole form is locked when sleep() is executed. Do you have any suggestion?

Thanks.

franz
22nd July 2009, 07:26
Try using a QTimeLine.

mcosta
22nd July 2009, 07:58
You can use a QTimer with interval of 1s and connect the "timeout()" signal with a slot that change background color

metdos
22nd July 2009, 08:05
I used QTimer.

for initialization


QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(1000);


and for stooping timer

timer->stop();

mcosta
22nd July 2009, 13:13
You must create a slot (ie. "changeColorSlot()") and connect it to timeout signal



void MyButton::changeColorSlot()
{
QColor currentColor = this->palette()->color(QPalette::Button);
QColor newColor;
if (currentColor == this->color1)
newColor = this->color2;
else
newColor = this->color1;

this->palette()->setColor(QPalette::Button, newColor);
}

Lykurg
22nd July 2009, 14:23
Hi, just a single optimization suggestion: I would use a int to determinate the state instead of querying the color:


private:
QColor m_color1;
QColor m_color2;
int m_buttonState;
//...
void MyButton::changeColorSlot()
{
m_buttonState = ++m_buttonState % 2
this->palette()->setColor(QPalette::Button, (m_buttonState) ? m_color1 : m_color2);
update();
}

wysota
22nd July 2009, 14:52
I would use a bool ;)

Lykurg
22nd July 2009, 15:36
I would use a bool ;)
Even better :o *shame*