Add a QPalette color role for placeholder texts
This allow to customize easily placeholders in QLineEdit by example. Change-Id: I2bb379164376e1d88b42d6c86c2e5b8df99fbc56 Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
This commit is contained in:
parent
4582a103c1
commit
ebd3a13b80
@ -315,6 +315,21 @@ static void qt_palette_from_color(QPalette &pal, const QColor &button)
|
||||
\sa ColorRole, brush()
|
||||
*/
|
||||
|
||||
/*!
|
||||
\fn const QBrush & QPalette::placeholderText() const
|
||||
\since 5.12
|
||||
|
||||
Returns the placeholder text brush of the current color group.
|
||||
|
||||
\note Before Qt 5.12, the placeholder text color was hard-coded in the code as
|
||||
QPalette::text().color() where an alpha of 128 was applied.
|
||||
We continue to support this behavior by default, unless you set your own brush.
|
||||
One can get back the original placeholder color setting the special QBrush default
|
||||
constructor as placeholder brush.
|
||||
|
||||
\sa ColorRole, brush()
|
||||
*/
|
||||
|
||||
/*!
|
||||
\fn ColorGroup QPalette::currentColorGroup() const
|
||||
|
||||
@ -444,6 +459,9 @@ static void qt_palette_from_color(QPalette &pal, const QColor &button)
|
||||
of QPalette, because tool tips are not active
|
||||
windows.
|
||||
|
||||
\value PlaceholderText Used as the placeholder color for various text input widgets.
|
||||
This enum value has been introduced in Qt 5.12
|
||||
|
||||
\value Text The foreground color used with \c Base. This is usually
|
||||
the same as the \c WindowText, in which case it must provide
|
||||
good contrast with \c Window and \c Base.
|
||||
@ -765,11 +783,32 @@ void QPalette::setBrush(ColorGroup cg, ColorRole cr, const QBrush &b)
|
||||
cg = Active;
|
||||
}
|
||||
|
||||
// For placeholder we want to continue to respect the original behavior, which is
|
||||
// derivating the text color, but only if user has not yet set his own brush.
|
||||
// We then use Qt::NoBrush as an inernal way to know if the brush is customized or not.
|
||||
|
||||
// ### Qt 6 - remove this special case
|
||||
// Part 1 - Restore initial color to the given color group
|
||||
if (cr == PlaceholderText && b == QBrush()) {
|
||||
QColor col = brush(Text).color();
|
||||
col.setAlpha(128);
|
||||
setBrush(cg, PlaceholderText, QBrush(col, Qt::NoBrush));
|
||||
return;
|
||||
}
|
||||
|
||||
if (d->br[cg][cr] != b) {
|
||||
detach();
|
||||
d->br[cg][cr] = b;
|
||||
}
|
||||
data.resolve_mask |= (1<<cr);
|
||||
|
||||
// ### Qt 6 - remove this special case
|
||||
// Part 2 - Update initial color to the given color group
|
||||
if (cr == Text && d->br[cg][PlaceholderText].style() == Qt::NoBrush) {
|
||||
QColor col = brush(Text).color();
|
||||
col.setAlpha(128);
|
||||
setBrush(cg, PlaceholderText, QBrush(col, Qt::NoBrush));
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
@ -962,7 +1001,7 @@ QDataStream &operator<<(QDataStream &s, const QPalette &p)
|
||||
for (int i = 0; i < NumOldRoles; ++i)
|
||||
s << p.d->br[grp][oldRoles[i]].color();
|
||||
} else {
|
||||
int max = QPalette::ToolTipText + 1;
|
||||
int max = (int)QPalette::NColorRoles;
|
||||
if (s.version() <= QDataStream::Qt_2_1)
|
||||
max = QPalette::HighlightedText + 1;
|
||||
else if (s.version() <= QDataStream::Qt_4_3)
|
||||
@ -1159,7 +1198,7 @@ QDebug operator<<(QDebug dbg, const QPalette &p)
|
||||
{"WindowText", "Button", "Light", "Midlight", "Dark", "Mid", "Text",
|
||||
"BrightText", "ButtonText", "Base", "Window", "Shadow", "Highlight",
|
||||
"HighlightedText", "Link", "LinkVisited", "AlternateBase", "NoRole",
|
||||
"ToolTipBase","ToolTipText" };
|
||||
"ToolTipBase","ToolTipText", "PlaceholderText" };
|
||||
QDebugStateSaver saver(dbg);
|
||||
QDebug nospace = dbg.nospace();
|
||||
const uint mask = p.resolve();
|
||||
|
@ -96,7 +96,8 @@ public:
|
||||
AlternateBase,
|
||||
NoRole,
|
||||
ToolTipBase, ToolTipText,
|
||||
NColorRoles = ToolTipText + 1,
|
||||
PlaceholderText,
|
||||
NColorRoles = PlaceholderText + 1,
|
||||
Foreground = WindowText, Background = Window
|
||||
};
|
||||
Q_ENUM(ColorRole)
|
||||
@ -141,6 +142,7 @@ public:
|
||||
inline const QBrush &highlightedText() const { return brush(HighlightedText); }
|
||||
inline const QBrush &link() const { return brush(Link); }
|
||||
inline const QBrush &linkVisited() const { return brush(LinkVisited); }
|
||||
inline const QBrush &placeholderText() const { return brush(PlaceholderText); }
|
||||
|
||||
bool operator==(const QPalette &p) const;
|
||||
inline bool operator!=(const QPalette &p) const { return !(operator==(p)); }
|
||||
|
@ -260,16 +260,16 @@ static int NColorRoles[] = {
|
||||
QPalette::HighlightedText + 1, // Qt_4_0, Qt_4_1
|
||||
QPalette::HighlightedText + 1, // Qt_4_2
|
||||
QPalette::AlternateBase + 1, // Qt_4_3
|
||||
QPalette::ToolTipText + 1, // Qt_4_4
|
||||
QPalette::ToolTipText + 1, // Qt_4_5
|
||||
QPalette::ToolTipText + 1, // Qt_4_6
|
||||
QPalette::ToolTipText + 1, // Qt_5_0
|
||||
QPalette::ToolTipText + 1, // Qt_5_1
|
||||
QPalette::ToolTipText + 1, // Qt_5_2
|
||||
QPalette::ToolTipText + 1, // Qt_5_3
|
||||
QPalette::ToolTipText + 1, // Qt_5_4
|
||||
QPalette::ToolTipText + 1, // Qt_5_5
|
||||
QPalette::ToolTipText + 1, // Qt_5_6
|
||||
QPalette::PlaceholderText + 1, // Qt_4_4
|
||||
QPalette::PlaceholderText + 1, // Qt_4_5
|
||||
QPalette::PlaceholderText + 1, // Qt_4_6
|
||||
QPalette::PlaceholderText + 1, // Qt_5_0
|
||||
QPalette::PlaceholderText + 1, // Qt_5_1
|
||||
QPalette::PlaceholderText + 1, // Qt_5_2
|
||||
QPalette::PlaceholderText + 1, // Qt_5_3
|
||||
QPalette::PlaceholderText + 1, // Qt_5_4
|
||||
QPalette::PlaceholderText + 1, // Qt_5_5
|
||||
QPalette::PlaceholderText + 1, // Qt_5_6
|
||||
0 // add the correct value for Qt_5_7 here later
|
||||
};
|
||||
|
||||
@ -2139,7 +2139,7 @@ void tst_QDataStream::setVersion()
|
||||
*/
|
||||
|
||||
// revise the test if new color roles or color groups are added
|
||||
QVERIFY(QPalette::NColorRoles == QPalette::ToolTipText + 1);
|
||||
QVERIFY(QPalette::NColorRoles == QPalette::PlaceholderText + 1);
|
||||
QCOMPARE(int(QPalette::NColorGroups), 3);
|
||||
|
||||
QByteArray ba2;
|
||||
|
@ -67,9 +67,10 @@ void tst_QPalette::roleValues_data()
|
||||
QTest::newRow("QPalette::NoRole") << int(QPalette::NoRole) << 17;
|
||||
QTest::newRow("QPalette::ToolTipBase") << int(QPalette::ToolTipBase) << 18;
|
||||
QTest::newRow("QPalette::ToolTipText") << int(QPalette::ToolTipText) << 19;
|
||||
QTest::newRow("QPalette::PlaceholderText") << int(QPalette::PlaceholderText) << 20;
|
||||
|
||||
// Change this value as you add more roles.
|
||||
QTest::newRow("QPalette::NColorRoles") << int(QPalette::NColorRoles) << 20;
|
||||
QTest::newRow("QPalette::NColorRoles") << int(QPalette::NColorRoles) << 21;
|
||||
}
|
||||
|
||||
void tst_QPalette::roleValues()
|
||||
|
Loading…
Reference in New Issue
Block a user