Make a QLabel blink

和自甴很熟 提交于 2021-01-27 16:06:57

问题


I'm using Qt to make a gui application for my beagleboard.

I'm trying to make a QLabel blink with a custom image.

QTimer::connect(timer, SIGNAL(timeout()), this, SLOT(blink()));
timer->start(1000);

I'm thinking to use QTimer to call the blink() function every second, but I do not have a clue what the code should be in blink() function. I hope someone can help, since i have struggle with this problem in a while now.


回答1:


Create blinking animation in a GIF file.

Create whatever animation you like with tools like GIF Animator.

Show it like below:

auto movie = new QMovie("myblinking.gif"); 
QLabel blinklabel = new QLabel();
blinklabel->setMovie(movie);
movie->start();



回答2:


The easiest way is to hide and show it again.

Class::blink()
{
    if(label->isHidden())
        label->show();
    else
        label->hide();
}

This approach is good because you don't need to set your image again and again, just show/hide it(set empty pixmap or set image every second is not efficient approach).

If you use layout, then it can really break your layout, so you can use QStackedWidget with imageLabel and empty label and change it every second. I think that it will be still better than set empty pixmap or set image every second. Choose the best for you.

http://qt-project.org/doc/qt-4.8/qstackedwidget.html




回答3:


Using a bool member isQLabelVisible in your class

Class::blink() {
    if(isQLabelVisible) {
        doHideQLabel();
        isQLabelVisible = false;
    } else {
        doShowQLabel();
        isQLabelVisible = true;
    }
}

void Class::doHideQLabel() {
    [...]
}

void Class::doShowQLabel() {
    [...]
}

You have a starting point for a good solution. Now, to implements do{Hide,Show}QLabel(), read answers from this question and decide which one is the best for your needs.



来源:https://stackoverflow.com/questions/26975329/make-a-qlabel-blink

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!