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. Why setting background color of QLineEdit has no effect?

Why setting background color of QLineEdit has no effect?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 4.9k 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.
  • T Offline
    T Offline
    Tapsa
    wrote on last edited by Tapsa
    #1

    I followed this guide and tried to change the background of QLineEdit object, but there is no change whatsoever. This is my code that should make the background color light green.

    QLineEdit *unit_search = new QLineEdit(catalog_units);
    QPalette pal(unit_search->palette());
    pal.setColor(QPalette::Background, QColor(220, 255, 220));
    unit_search->setAutoFillBackground(true);
    unit_search->setPalette(pal);
    

    I am using Qt Creator 3.6.0 based on Qt 5.5.1.
    The only difference with my other project that has working background colors is that this project has no design forms. Native events also do not work with this project, even 100 % identical code to another project in where they work.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Tapsa said:
      Hi

      try this
      QPalette palette;
      palette.setColor(QPalette::Base,QColor(220, 255, 220));
      unit_search ->setPalette(palette);

      It seems LineEdit uses that and not QPalette::Background.

      T 1 Reply Last reply
      1
      • mrjjM mrjj

        @Tapsa said:
        Hi

        try this
        QPalette palette;
        palette.setColor(QPalette::Base,QColor(220, 255, 220));
        unit_search ->setPalette(palette);

        It seems LineEdit uses that and not QPalette::Background.

        T Offline
        T Offline
        Tapsa
        wrote on last edited by
        #3

        @mrjj Thanks. It works.

        mrjjM 1 Reply Last reply
        0
        • T Tapsa

          @mrjj Thanks. It works.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Tapsa
          super :)

          Just a side note: if you want this for all your lineEdits then
          a style sheet might be easier. Like in having this color all the time.

          But if to use for ok / invalid etc or something like that, then palette is fine.

          1 Reply Last reply
          1
          • S Offline
            S Offline
            Sakhana99
            wrote on last edited by
            #5

            I still can't change the background color of my QLineEdit. I do the same thing as you do and nothing happens.

            mrjjM 1 Reply Last reply
            0
            • S Sakhana99

              I still can't change the background color of my QLineEdit. I do the same thing as you do and nothing happens.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Sakhana99
              It would then be good to list what platform(lin,win,mac) and what Qt version(Qt5.13,Qt6 etc) you are trying this on as things change in 7 years :)

              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