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. StyleSheet to change QLineEdit background while being edited
Forum Updated to NodeBB v4.3 + New Features

StyleSheet to change QLineEdit background while being edited

Scheduled Pinned Locked Moved Unsolved General and Desktop
qlineeditstylesheet
4 Posts 3 Posters 3.8k Views 2 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.
  • shaveraS Offline
    shaveraS Offline
    shavera
    wrote on last edited by shavera
    #1

    It is a concern in one of my projects that a user may edit text (entering a number, say), but then not leave the line edit, nor hit enter, and thus 'textEdited' is not emitted. I'd like to emphasize that the value on the screen does not match the value deeper in the software (that would be modified upon the 'textEdited' signal).

    Currently, I'm changing the background color of the QLineEdit in each object separately. eg:

    void MyWidget::on_lineEdit_1_textEdited(){ 
      ui->lineEdit_1->setStyleSheet("background: pink"); 
    }
    void MyWidget::on_lineEdit_1_editingFinished() { 
      ui->lineEdit_1->setStyleSheet("");
      backendThing->setValue1(ui->lineEdit_1->text()->toDouble());
    }
    

    But it's pretty onerous to have that code for each and every line edit box. What would be ideal is if QLineEdit had some state such that a style sheet could be

    QLineEdit:whileEditing {background: pink}
    

    My current stupid hack way will probably be to subclass QLineEdit, add in a dynamic property, then have stylesheet hook on that dynamic property? Is there a better solution out there?

    1 Reply Last reply
    0
    • shaveraS Offline
      shaveraS Offline
      shavera
      wrote on last edited by
      #2

      followup: I've tried the following:

      class MyLineEdit : public QLineEdit
      {
          Q_OBJECT
      public:
          MyLineEdit(QWidget* parent = 0);
      
      private slots:
          void onTextEdited();
          void onEditingFinished();
      };
      
      MyLineEdit::MyLineEdit(QWidget* parent)
          : QLineEdit(parent)
      {
          setProperty("whileEditing", false);
      
          connect(this, &QLineEdit::textEdited,
                  this, &MyLineEdit::onTextEdited);
          connect(this, &QLineEdit::editingFinished,
                  this, &MyLineEdit::onEditingFinished);
      }
      
      void MyLineEdit::onTextEdited()
      {
          setProperty("whileEditing", true);
          qDebug() << "a" << property("whileEditing").toString();
      }
      
      void MyLineEdit::onEditingFinished()
      {
          setProperty("whileEditing", false);
          qDebug() << "b" << property("whileEditing").toString();
      }
      
      

      and set the main window stylesheet to:

      this->setStyleSheet("*[whileEditing=\"true\"]{background-color: pink}");
      

      But no change in background.

      I then changed 'whileEditing' to true at construction time, and it changed the background to pink but then did not change back after editing finished.

      it seems that stylesheet will set background once and then not pay attention to dynamic property changes later.
      So really, I think QLineEdit needs to have a state that you can query like "hover" or "focus" that indicates when its state is intermediate between 'textEdited()' and 'editingFinished()'

      CharbyC 1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        Something you can do when you change your property is to call.

        style()->unpolish(this);
        style()->polish(this);
        

        Hope it helps

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • shaveraS shavera

          followup: I've tried the following:

          class MyLineEdit : public QLineEdit
          {
              Q_OBJECT
          public:
              MyLineEdit(QWidget* parent = 0);
          
          private slots:
              void onTextEdited();
              void onEditingFinished();
          };
          
          MyLineEdit::MyLineEdit(QWidget* parent)
              : QLineEdit(parent)
          {
              setProperty("whileEditing", false);
          
              connect(this, &QLineEdit::textEdited,
                      this, &MyLineEdit::onTextEdited);
              connect(this, &QLineEdit::editingFinished,
                      this, &MyLineEdit::onEditingFinished);
          }
          
          void MyLineEdit::onTextEdited()
          {
              setProperty("whileEditing", true);
              qDebug() << "a" << property("whileEditing").toString();
          }
          
          void MyLineEdit::onEditingFinished()
          {
              setProperty("whileEditing", false);
              qDebug() << "b" << property("whileEditing").toString();
          }
          
          

          and set the main window stylesheet to:

          this->setStyleSheet("*[whileEditing=\"true\"]{background-color: pink}");
          

          But no change in background.

          I then changed 'whileEditing' to true at construction time, and it changed the background to pink but then did not change back after editing finished.

          it seems that stylesheet will set background once and then not pay attention to dynamic property changes later.
          So really, I think QLineEdit needs to have a state that you can query like "hover" or "focus" that indicates when its state is intermediate between 'textEdited()' and 'editingFinished()'

          CharbyC Offline
          CharbyC Offline
          Charby
          wrote on last edited by
          #4

          @shavera Did you tried to call QWidget::Update() after modifing your whileEditing property ?

          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