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. [SOLVED]Stylesheet - changing background color when text is entered in QLineEdit
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]Stylesheet - changing background color when text is entered in QLineEdit

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 4.2k 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.
  • C Offline
    C Offline
    chris17
    wrote on last edited by
    #1

    Hi,

    in my application i want to highlight mandatory fields when they are empty.

    at the moment i'm doing it like this:
    @
    MainWindow w;
    w.setStyleSheet("*[mandatoryField="true"][text=""] { background-color: yellow }");
    @
    @
    //leInput is a QLineEdit
    ui->leInput->setProperty("mandatoryField",true);
    @

    This hightlights all mandatory fields, which are empty, but when the user enters a text in the field i would like to set the background-color back to normal.

    Is it even possible for stylesheets to detect when the text changes?

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      you will have to do the check by yourself.
      As soon as the user changes the text you need to do something like this:
      @
      if( ui->leInput->property("mandatoryField").toBool() )
      {
      ui->leInput->setProperty("mandatoryField",false);
      ui->leInput->style()->polish(ui->leInput);
      }
      @

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Sam
        wrote on last edited by
        #3

        Check this "doc note":http://qt-project.org/doc/qt-5.0/qtwidgets/stylesheet-syntax.html#note-238

        1 Reply Last reply
        0
        • C Offline
          C Offline
          chris17
          wrote on last edited by
          #4

          Thanks for the suggestions, it works

          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