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. Where is QLineEdit's text drawn in QStyle?
QtWS25 Last Chance

Where is QLineEdit's text drawn in QStyle?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qpaletteqstyle
3 Posts 2 Posters 689 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.
  • C Offline
    C Offline
    CJha
    wrote on 23 Jul 2021, 13:02 last edited by CJha
    #1

    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 of QLineEdit always remains black. How can I change the text colour of QLineEdit?

    P.S: Why is it that sometimes QStyleuses QPalette::Button as background colour and QPalette::WindowText as text colour when QPalette::ButtonText is already a defined colour (e.g. in QTabBar)? This can be really inconvenient if the two colours (QPalette::Window and QPalette::Button) are of opposite shades.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 23 Jul 2021, 13:16 last edited by
      #2

      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 2040

      p.setPen(pal.text().color());

      C 1 Reply Last reply 23 Jul 2021, 13:24
      0
      • M mrjj
        23 Jul 2021, 13:16

        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 2040

        p.setPen(pal.text().color());

        C Offline
        C Offline
        CJha
        wrote on 23 Jul 2021, 13:24 last edited by CJha
        #3

        @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);
        	}
        }
        1 Reply Last reply
        1

        2/3

        23 Jul 2021, 13:16

        • Login

        • Login or register to search.
        2 out of 3
        • First post
          2/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved