Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Moved] Blink a QLabel

[Moved] Blink a QLabel

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 7.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dpatrick
    wrote on last edited by
    #1

    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 }");
    }
    ui->lblA->repaint();
    ui->lblB->repaint();
    }
    @

    Any ideas?

    Thank all

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tony
      wrote on last edited by
      #2

      Hi,

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

      Here's the code:

      @
      class Label : public QLabel
      {
      Q_OBJECT

      protected:
      void keyPressEvent(QKeyEvent *event);

      public:
      Label(QWidget *parent = 0);
      };
      @

      @
      Label::Label(QWidget *parent) :
      QLabel(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);");

      QLabel::keyPressEvent(event);
      }
      @

      Maybe in your case the style sheet text is wrong.

      Tony.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dpatrick
        wrote on last edited by
        #3

        Thanks a lot.
        It is working

        1 Reply Last reply
        0
        • ? This user is from outside of this forum
          ? This user is from outside of this forum
          Guest
          wrote on last edited by
          #4

          Moved to desktop forum

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved