How do I make a QLabel Blink?
-
The consensus is evidently to l hide and show a QLabel using a Qtimer. I tried this using a single-shot Qtimer with a QLabel on a MainWidget using Qt 6.1.2 running on Windows 10. This didn't work for me. Here's what I did:
void BJTMainWindow::showYouBust() { QTimer::singleShot(3000, this, &BJTMainWindow::updateCaption); QTimer::singleShot(3000, this, &BJTMainWindow::updateCaption); QTimer::singleShot(3000, this, &BJTMainWindow::updateCaption); QTimer::singleShot(3000, this, &BJTMainWindow::updateCaption); QTimer::singleShot(3000, this, &BJTMainWindow::updateCaption); } void BJTMainWindow::updateCaption() { if (myBjtMW->youBustLabel->isVisible()) { myBjtMW->youBustLabel->hide(); } else { myBjtMW->youBustLabel->show(); } }
After a delay (of 15 seconds?). The label shows once. I ran this through gdb. The hides and shows are definitely being called. What am I missing?
-
@DougyDrumz2 said in How do I make a QLabel Blink?:
QTimer::singleShot(3000, this, &BJTMainWindow::updateCaption);
QTimer::singleShot(3100, this, &BJTMainWindow::updateCaption);
QTimer::singleShot(3200, this, &BJTMainWindow::updateCaption);
QTimer::singleShot(3300, this, &BJTMainWindow::updateCaption);
QTimer::singleShot(3400, this, &BJTMainWindow::updateCaption); -
@DougyDrumz2 said in How do I make a QLabel Blink?:
QTimer::singleShot(3000, this, &BJTMainWindow::updateCaption);
QTimer::singleShot(3000, this, &BJTMainWindow::updateCaption);
QTimer::singleShot(3000, this, &BJTMainWindow::updateCaption);
QTimer::singleShot(3000, this, &BJTMainWindow::updateCaption);
QTimer::singleShot(3000, this, &BJTMainWindow::updateCaption);this calls the method 5 times in a row after 3 seconds, not after 3,6,9,12,15s as you expect.
-
Awesome. Thanks
-
@DougyDrumz2 said in How do I make a QLabel Blink?:
The consensus is evidently to l hide and show a QLabel
Not my "consensus :) So far as I am aware, hiding can cause different layout redraw in at least some circumstances. I retain visibility but toggle foreground color to
QColor(Qt::transparent)
or toggle alpha color value between255
(opaque) and0
(transparent) on paletteQPalette::setColor(QPalette::Text, colour)
.Also, creating multiple
QTimer::singleShot()
s doesn't scale very well if you want to change the number of flashes or the interval between. Or of you have multiple labels to blink and you'd like them all to be "in sync". I use one regular repeatingQTimer
with a "countdown" and cancellation when it reaches 0.Up to you on both of these.