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. How to do a color animation of a QLineEdit background
Forum Updated to NodeBB v4.3 + New Features

How to do a color animation of a QLineEdit background

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 5 Posters 4.1k Views 1 Watching
  • 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.
  • J Offline
    J Offline
    JesusM
    wrote on last edited by
    #1

    Hi, I want to change the background of a QLineEdit in order to provide feedback to the user so, I change it to green if the text introduced is correct and red if it's incorrect but, I don't want the color to persist all the time. I just want to change the color 1 second and then come back to the default color background.

    How can I do it?. I was thinking about using QPropertyAnimation but I read It doesn't accept QString as property for animation.

    Thanks.

    jsulmJ A 2 Replies Last reply
    0
    • J JesusM

      Hi, I want to change the background of a QLineEdit in order to provide feedback to the user so, I change it to green if the text introduced is correct and red if it's incorrect but, I don't want the color to persist all the time. I just want to change the color 1 second and then come back to the default color background.

      How can I do it?. I was thinking about using QPropertyAnimation but I read It doesn't accept QString as property for animation.

      Thanks.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @JesusM said in How to do a color animation of a QLineEdit background:

      but I read It doesn't accept QString as property for animation

      You want to animate color not string, so no problem. You need to anymate background color.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      J 1 Reply Last reply
      0
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #3

        QPropertyAnimation is based on QVariantAnimation, which does support QColor: https://doc.qt.io/qt-5/qvariantanimation.html. You can add custom types there as well (including QString), if you want to - check the example in class description.

        (Z(:^

        J 1 Reply Last reply
        2
        • jsulmJ jsulm

          @JesusM said in How to do a color animation of a QLineEdit background:

          but I read It doesn't accept QString as property for animation

          You want to animate color not string, so no problem. You need to anymate background color.

          J Offline
          J Offline
          JesusM
          wrote on last edited by
          #4

          @jsulm oh, I thought what I had to animate was the stylesheet so I needed to introduce a QString. I will try that.

          Thanks

          1 Reply Last reply
          0
          • sierdzioS sierdzio

            QPropertyAnimation is based on QVariantAnimation, which does support QColor: https://doc.qt.io/qt-5/qvariantanimation.html. You can add custom types there as well (including QString), if you want to - check the example in class description.

            J Offline
            J Offline
            JesusM
            wrote on last edited by
            #5

            @sierdzio ok, the answer I found it's a bit old so now It seems I can use also QString. I will try.
            Thanks!

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              You can set QLineEdit background using setPalette():

              const QColor(Qt::Red);
              QPalette palette(widget->palette());
              palette.setBrush(QPalette::Button, QBrush(color));
              palette.setBrush(QPalette::Base, QBrush(color));
              widget->setPalette(palette);
              

              Stylesheet is an option, too, but in this case seems like an overkill (and will likely be slower).

              (Z(:^

              J 1 Reply Last reply
              1
              • sierdzioS sierdzio

                You can set QLineEdit background using setPalette():

                const QColor(Qt::Red);
                QPalette palette(widget->palette());
                palette.setBrush(QPalette::Button, QBrush(color));
                palette.setBrush(QPalette::Base, QBrush(color));
                widget->setPalette(palette);
                

                Stylesheet is an option, too, but in this case seems like an overkill (and will likely be slower).

                J Offline
                J Offline
                JesusM
                wrote on last edited by JesusM
                #7

                @sierdzio but I need to do it with the QPropertyAnimation so I cant use QPalette as QVariant as it is not supported. I tried this:

                checkIaAnimation = new QPropertyAnimation(ui->IaValue,"palette",this);
                    checkIaAnimation->setDuration(1000);
                    checkIaAnimation->setStartValue(QColor(Qt::white));
                    checkIaAnimation->setEndValue(QColor(Qt::green));
                    checkIaAnimation->setDirection(QPropertyAnimation::Forward);
                    checkIaAnimation->start();
                

                It doesn't crash but It does nothing, as is logical, because I am not specifying which color of the palette I want to change (background) but I don't know how to specify it.

                D 1 Reply Last reply
                0
                • sierdzioS Offline
                  sierdzioS Offline
                  sierdzio
                  Moderators
                  wrote on last edited by
                  #8

                  Ah, right, makes sense. QPalette is not even a QObject so it does not have properties.

                  So I think that leaves 2 options:

                  • write custom interpolator for QPaletter and register it with qRegisterAnimationInterpolator and your current code should start working
                  • subclass QLineEdit and add a QColor property that will update the background palette when modified, then animate that property

                  (Z(:^

                  1 Reply Last reply
                  1
                  • J JesusM

                    @sierdzio but I need to do it with the QPropertyAnimation so I cant use QPalette as QVariant as it is not supported. I tried this:

                    checkIaAnimation = new QPropertyAnimation(ui->IaValue,"palette",this);
                        checkIaAnimation->setDuration(1000);
                        checkIaAnimation->setStartValue(QColor(Qt::white));
                        checkIaAnimation->setEndValue(QColor(Qt::green));
                        checkIaAnimation->setDirection(QPropertyAnimation::Forward);
                        checkIaAnimation->start();
                    

                    It doesn't crash but It does nothing, as is logical, because I am not specifying which color of the palette I want to change (background) but I don't know how to specify it.

                    D Offline
                    D Offline
                    Devopia53
                    wrote on last edited by
                    #9

                    @JesusM

                    In that case, QTimer is appropriate, but if you need animation, you can use QTimeLine as well. like this:

                        auto    timeLine = new QTimeLine(1000, this);
                        timeLine->setFrameRange(0, 255);
                        connect(timeLine, &QTimeLine::frameChanged, [=](int frame){
                            auto    p = ui->IaValue->palette();
                            p.setBrush(QPalette::Base, QBrush(QColor(frame, frame, frame)));
                           ui->IaValue->setPalette(p);
                        });
                        connect(timeLine, &QTimeLine::finished, timeLine, &QTimeLine::deleteLater);
                        timeLine->start();
                    
                    sierdzioS 1 Reply Last reply
                    1
                    • D Devopia53

                      @JesusM

                      In that case, QTimer is appropriate, but if you need animation, you can use QTimeLine as well. like this:

                          auto    timeLine = new QTimeLine(1000, this);
                          timeLine->setFrameRange(0, 255);
                          connect(timeLine, &QTimeLine::frameChanged, [=](int frame){
                              auto    p = ui->IaValue->palette();
                              p.setBrush(QPalette::Base, QBrush(QColor(frame, frame, frame)));
                             ui->IaValue->setPalette(p);
                          });
                          connect(timeLine, &QTimeLine::finished, timeLine, &QTimeLine::deleteLater);
                          timeLine->start();
                      
                      sierdzioS Offline
                      sierdzioS Offline
                      sierdzio
                      Moderators
                      wrote on last edited by
                      #10

                      Yep, that's also a neat idea.

                      @Devopia53 said in How to do a color animation of a QLineEdit background:

                      connect(timeLine, &QTimeLine::frameChanged, [=](int frame){

                      There should be a control object for this labda, so:

                       connect(timeLine, &QTimeLine::frameChanged, this, [=](int frame){
                      

                      (Z(:^

                      1 Reply Last reply
                      0
                      • J JesusM

                        Hi, I want to change the background of a QLineEdit in order to provide feedback to the user so, I change it to green if the text introduced is correct and red if it's incorrect but, I don't want the color to persist all the time. I just want to change the color 1 second and then come back to the default color background.

                        How can I do it?. I was thinking about using QPropertyAnimation but I read It doesn't accept QString as property for animation.

                        Thanks.

                        A Offline
                        A Offline
                        anil_arise
                        wrote on last edited by
                        #11

                        @JesusM May be like that type result ::

                        connect(ui->lineEdit,SIGNAL(textChanged(QString)),this,SLOT(ChangeColor(QString)));

                        ChangeColor(QString text)
                        {
                        if(text.size()==0)
                        ui->lineEdit->setStyleSheet("background-color:none; color:none;");
                        else if(text.size()>10)
                        ui->lineEdit->setStyleSheet("background-color:rgb(255,0,0); color:rgb(255,255,255);");
                        else
                        ui->lineEdit->setStyleSheet("background-color:rgb(0,170,0); color:rgb(255,255,0);");

                        }

                        1 Reply Last reply
                        0
                        • J Offline
                          J Offline
                          JesusM
                          wrote on last edited by
                          #12

                          Thanks all of you. Your answers gave me the solution! Now It works fine!

                          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