Where is QLineEdit's text drawn in QStyle?
-
Hi, I am trying to create my own style, I am using Fusion style as my base style to do so. My main motive is to change the background and text colours of different widgets. I am unable to change the text colour for
QLineEdit
. This is what I am doing:void GuiStyle::drawPrimitive(QStyle::PrimitiveElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget) const { if(element == QStyle::PE_PanelLineEdit) { const QStyleOptionFrame* comboOption = qstyleoption_cast<const QStyleOptionFrame*>(option); QStyleOptionFrame newComboOption = *comboOption; newComboOption.palette.setBrush(QPalette::Base, Qt::red); newComboOption.palette.setBrush(QPalette::Text, Qt::green); newComboOption.palette.setBrush(QPalette::WindowText, Qt::green); newComboOption.palette.setBrush(QPalette::ButtonText, Qt::green); newComboOption.palette.setBrush(QPalette::BrightText, Qt::green); newComboOption.palette.setBrush(QPalette::ToolTipText, Qt::green); QProxyStyle::drawPrimitive(element, &newComboOption, painter, widget); } else if(element == QStyle::PE_FrameLineEdit) { const QStyleOptionFrame* comboOption = qstyleoption_cast<const QStyleOptionFrame*>(option); QStyleOptionFrame newComboOption = *comboOption; newComboOption.palette.setBrush(QPalette::Base, Qt::red); newComboOption.palette.setBrush(QPalette::Window, Qt::green); newComboOption.palette.setBrush(QPalette::Text, Qt::green); newComboOption.palette.setBrush(QPalette::WindowText, Qt::green); newComboOption.palette.setBrush(QPalette::BrightText, Qt::green); newComboOption.palette.setBrush(QPalette::ToolTipText, Qt::green); QProxyStyle::drawPrimitive(element, &newComboOption, painter, widget); } else { QProxyStyle::drawPrimitive(element, option, painter, widget); } }
The base colour is red, but no matter what text colour I change (as you can see above, I have changed all text colours I could find to
Qt::green
) the text colour ofQLineEdit
always remains black. How can I change the text colour ofQLineEdit
?P.S: Why is it that sometimes
QStyle
usesQPalette::Button
as background colour andQPalette::WindowText
as text colour whenQPalette::ButtonText
is already a defined colour (e.g. inQTabBar
)? This can be really inconvenient if the two colours (QPalette::Window
andQPalette::Button
) are of opposite shades. -
Hi
It seems to use the Widgets palette and not the newComboOption.palette
https://code.woboq.org/qt5/qtbase/src/widgets/widgets/qlineedit.cpp.html
line 2040p.setPen(pal.text().color());
-
@mrjj Thanks, it seems like odd behaviour. So, it uses its native palette, i.e. the palette set either directly or inherited from its parent. I am able to change the colour now, but I am using a C-Style cast, and that's why I am not sure if I should do it. Here is my solution:
void GuiStyle::drawPrimitive(QStyle::PrimitiveElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget) const { if(vars[id].customTheme) { if(element == QStyle::PE_PanelLineEdit) { const QStyleOptionFrame* comboOption = qstyleoption_cast<const QStyleOptionFrame*>(option); QStyleOptionFrame newComboOption = *comboOption; newComboOption.palette.setBrush(QPalette::Base, Qt::red); // I am using C-Style cast to get the widget in a non-const pointer and then it's palette again the same way QLineEdit* lineEditPtr = (QLineEdit*)widget; QPalette pal = (QPalette)lineEditPtr->palette(); pal.setColor(QPalette::Text, Qt::green); lineEditPtr->setPalette(pal); QProxyStyle::drawPrimitive(element, &newComboOption, painter, widget); } } else { QProxyStyle::drawPrimitive(element, option, painter, widget); } }