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?

Where is QLineEdit's text drawn in QStyle?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qpaletteqstyle
3 Posts 2 Posters 709 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.
  • CJhaC Offline
    CJhaC Offline
    CJha
    wrote on 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
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on 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());

      CJhaC 1 Reply Last reply
      0
      • mrjjM mrjj

        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());

        CJhaC Offline
        CJhaC Offline
        CJha
        wrote on 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

        • Login

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