Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    [Moved] Blink a QLabel

    General and Desktop
    2
    4
    7491
    Loading More Posts
    • 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
      dpatrick last edited by

      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 Reply Quote 0
      • T
        tony last edited by

        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 Reply Quote 0
        • D
          dpatrick last edited by

          Thanks a lot.
          It is working

          1 Reply Last reply Reply Quote 0
          • ?
            Guest last edited by

            Moved to desktop forum

            1 Reply Last reply Reply Quote 0
            • First post
              Last post