[Moved] Blink a QLabel

  • Hi All,

    I would like to change the background of a QLabel after an event.

    With qt-creator’s designer, I created a QLabel and I adjusted the style sheet of the QLabel like this:
    QLabel {
    Background: blue;

    So, when I receive an event, I try to adjust the background color of the QLabel, but it doesn't change. Please see code below:

    void Form1::KeyPressEvent(QKeyEvent* event){
    if (event->key() == Qt::Key_0) {
    ui->lblA->setStyleSheet("QLabel {background: red }");
    ui->lblB->setStyleSheet("QLabel {background: blue }");
    } else if (event->key() == Qt::Key_1) {
    ui->lblA->setStyleSheet("QLabel {background: blue }");
    ui->lblB->setStyleSheet("QLabel {background: red }");

    Any ideas?

    Thank all

  • Hi,

    well, I've just made a test and it works.

    Here's the code:

    class Label : public QLabel

    void keyPressEvent(QKeyEvent *event);

    Label(QWidget *parent = 0);

    Label::Label(QWidget *parent) :
    setText("Hello world!");

    void Label::keyPressEvent(QKeyEvent *event)
    if (event->key() == Qt::Key_0)
    setStyleSheet("background-color: rgb(255,0,0);");
    else if (event->key() == Qt::Key_1)
    setStyleSheet("background-color: rgb(0,0,255);");


    Maybe in your case the style sheet text is wrong.


  • Thanks a lot.
    It is working

  • Moved to desktop forum

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.