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. What is the best way to apply multiple simultaneous background colors plus other formatting to a QLineEdit-like widget?
Forum Update on Monday, May 27th 2025

What is the best way to apply multiple simultaneous background colors plus other formatting to a QLineEdit-like widget?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 415 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.
  • Z Offline
    Z Offline
    ZeHgS
    wrote on last edited by ZeHgS
    #1

    Hi everyone!

    I need a QLineEdit (or equivalent) that will allow me to paint the background different colors depending on the total character length of the section so that, for example, anything up to 60 characters is colored green, anything up to 75 is colored yellow and anything above is colored red, etc.

    What is the best way to do this? Is there a built-in method easier than creating a single-lined QTextEdit? If not, how would I go about creating it? Simply fixating the height and setting it to no wrap?

    Thanks a lot!

    EDIT:

    Like so:

    ecd8eb45-14ab-493c-83f1-348dace13355-image.png

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by ChrisW67
      #2

      There are several ways. One is to use the QLineEdit::textChanged() signal and a style sheet:

      #ifndef FANCYLINEDIT_H
      #define FANCYLINEDIT_H
      #include <QLineEdit>
      
      class FancyLineEdit : public QLineEdit
      {
          Q_OBJECT
      public:
          FancyLineEdit(QWidget *parent = nullptr);
          ~FancyLineEdit();
      private slots:
          void adjustBackground(const QString &text);
      };
      #endif // FANCYLINEDIT_H
      
      #include "fancylineedit.h"
      
      FancyLineEdit::FancyLineEdit(QWidget *parent)
          : QLineEdit(parent)
      {
          connect(this, &FancyLineEdit::textChanged, this, &FancyLineEdit::adjustBackground);
      }
      
      FancyLineEdit::~FancyLineEdit()
      {
      }
      
      void FancyLineEdit::adjustBackground(const QString &text)
      {
          if (text.length() <= 60) {
              // No stylesheet: default colouring
              setStyleSheet("");
          }
          else if (text.length() <= 75) {
              setStyleSheet("QLineEdit { background-color: yellow }");
          }
          else {
              setStyleSheet("QLineEdit { background-color: red }");
          }
      }
      
      1 Reply Last reply
      3
      • Z Offline
        Z Offline
        ZeHgS
        wrote on last edited by
        #3

        Thank you so much for replying!

        What I actually need is to simultaneously have different colors for each of the sections like below:

        92b41d87-c51e-4d04-8ec0-cd839a25e2f7-image.png

        Can that be accomplished with stylesheets?

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

          Not in a simple way.

          You might be able to approximate that with a qlineargradient as the background,

                  setStyleSheet("QLineEdit { background-color: qlineargradient( "
                                        "x1:0 y1:0, x2:1 y2:0, "
                                        "stop:0.000 palette(base),  stop:0.499 palette(base), "
                                        "stop:0.500 yellow, stop:0.749 yellow, "
                                        "stop:0.750 red,    stop:1.000 red "
                                        ")}");
          

          but you will be faced with the task of determining where the stops in the gradient should be to align with the characters in a proportional font.

          At that point you may be better off overriding paintEvent(), computing where the various sub-sections of the text fall, and rendering the background yourself.

          jeremy_kJ 1 Reply Last reply
          4
          • C ChrisW67

            Not in a simple way.

            You might be able to approximate that with a qlineargradient as the background,

                    setStyleSheet("QLineEdit { background-color: qlineargradient( "
                                          "x1:0 y1:0, x2:1 y2:0, "
                                          "stop:0.000 palette(base),  stop:0.499 palette(base), "
                                          "stop:0.500 yellow, stop:0.749 yellow, "
                                          "stop:0.750 red,    stop:1.000 red "
                                          ")}");
            

            but you will be faced with the task of determining where the stops in the gradient should be to align with the characters in a proportional font.

            At that point you may be better off overriding paintEvent(), computing where the various sub-sections of the text fall, and rendering the background yourself.

            jeremy_kJ Offline
            jeremy_kJ Offline
            jeremy_k
            wrote on last edited by
            #5

            @ChrisW67 said in What is the best way to apply multiple simultaneous background colors plus other formatting to a QLineEdit-like widget?:

            but you will be faced with the task of determining where the stops in the gradient should be to align with the characters in a proportional font.

            QFontMetrics is the tool for figuring out this piece.

            Asking a question about code? http://eel.is/iso-c++/testcase/

            1 Reply Last reply
            3
            • Z Offline
              Z Offline
              ZeHgS
              wrote on last edited by
              #6

              Thank you both so much!! This seems like a really cool solution, I'll implement it later today! Thanks again

              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