Qt Forum

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

    Solved Why setting background color of QLineEdit has no effect?

    General and Desktop
    3
    6
    3794
    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.
    • T
      Tapsa last edited by Tapsa

      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 Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by

        @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 Reply Quote 1
        • T
          Tapsa @mrjj last edited by

          @mrjj Thanks. It works.

          mrjj 1 Reply Last reply Reply Quote 0
          • mrjj
            mrjj Lifetime Qt Champion @Tapsa last edited by

            @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 Reply Quote 1
            • S
              Sakhana99 last edited by

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

              mrjj 1 Reply Last reply Reply Quote 0
              • mrjj
                mrjj Lifetime Qt Champion @Sakhana99 last edited by

                @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 Reply Quote 0
                • First post
                  Last post