What is the best way to apply multiple simultaneous background colors plus other formatting to a QLineEdit-like widget?
-
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:
-
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 }"); } }
-
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.
-
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.
@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.