0

I want to show red animated blinking icon in Qt C++ application.

Display a red blinking icon when a variable is set, when a variable is clear display the green blinking icon.

This will be continuous if disconnected - blink the red icon, connected - blink the green icon.

Can anyone tell me how can I display it.

tharunkumar
  • 2,801
  • 1
  • 16
  • 19
  • 1
    There are many ways to display an image. For example [`QLabel::setPixmap`](http://doc.qt.io/qt-5/qlabel.html#pixmap-prop). Your question is too broad as it is. Narrow the problem down. What are you having trouble with exactly? – thuga May 03 '16 at 08:43
  • can you provide a sample code to display the animated image [not normal image] – tharunkumar May 03 '16 at 08:46
  • What is the format of this animated image? – thuga May 03 '16 at 08:52
  • anything which should be work with the Qt application – tharunkumar May 03 '16 at 09:03
  • Well then you just have to follow the answer provided by jpo38. Use a timer to control the blinking, and some variable to check if it should be green or red. – thuga May 03 '16 at 09:32

2 Answers2

1

Once you know how to display an imag: QImage, or QIcon, or QPixmap, it's pretty easy to have it be animated by blinking:

  • Create first QImage (or QIcon, or QPixmap) (green)
  • Create second QImage (or QIcon, or QPixmap) (red)
  • Create and start a QTimer and connect timeout() signal to a slot that will switch between the two first images
jpo38
  • 20,821
  • 10
  • 70
  • 151
  • I want to display a red icon blinking on a particular variable set and when clear show the green icon blinking – tharunkumar May 03 '16 at 08:49
  • Then you definitely need to detail your question. However, my answer should help you achieving that (make the red image blink, then it's pretty simple to switch it to non blinking green image when done). – jpo38 May 03 '16 at 08:52
  • it's probably better to update image not by `timerEvent` or `timeout`, but by signal, notifying changed state – Andrei R. May 03 '16 at 09:39
  • @AndreiR. Except if you want the change the image every n milliseconds (to have it blink). – jpo38 May 03 '16 at 09:40
0

Using the timer and QMovie I have solved the Issue.

Below is the timer to update the Notification bar

  updateScreenTimer = new QTimer(this);
  connect(updateScreenTimer, SIGNAL(timeout()), this, SLOT(update_Time_Slot()));
  updateScreenTimer->start(TIME_USED_UPDATE_NOTIFICATION);

After every TIME_USED_UPDATE_NOTIFICATION timeout, it will call the below slot and checks for the variable, if variable is set start the green blinking animated gif image movie. else start the red blinking animated gif image.

    void Notification::update_Time_Slot()
    {    
        if(ConectionStatus==1)
        {
            QMovie *movie = new QMovie(":/new/prefix1/greenBlinking1.gif");
            if(movie->isValid())
            {
                ui->connectionLabel->setMovie(movie);
                movie->start();
            }
            ui->connectionLabel->show();
        }
        else
        {
            QMovie *movie = new QMovie(":/new/prefix1/redBlinking1.gif");
            if(movie->isValid())
            {
                ui->connectionLabel->setMovie(movie);
                movie->start();
            }
            ui->connectionLabel->show();
        }
    }

Related posts -> What is the best way to display an animated icon in a QTableView?

Community
  • 1
  • 1
tharunkumar
  • 2,801
  • 1
  • 16
  • 19
  • Why don't you just call `update_Time_Slot` when the value of `ConectionStatus` is changed? – thuga May 04 '16 at 10:01
  • That's a huge chunk of code that could be factorized. Just create the two movies somewhere, keep them alive in your app forever, then apply them. No need to create a new `QMovie` every time. No need to have two large code blocks when everything is equal except the path to the movie. – Guimoute Feb 20 '23 at 12:17