PDA

View Full Version : Custom QLineEdit....highlighting



Plissken
3rd July 2012, 02:34
I have a class that inherits QLineEdit. It overrides the paintEvent method to draw a custom look. For the original QLineEdit, you can highlight text with a mouse button drag, and I assume on most systems it's a blue highlight with white text. My custom QLineEdit widget can paint the text and it can select text using the mouse, but no highlight ever shows up. It is like it is invisible. Any suggestions on how to get the highlighting to work? If I have to do it manually inside paintEvent where do I start?

ChrisW67
3rd July 2012, 05:39
Yes, it would be done by the paint event of QLineEdit using the style options. The place to start would be the Qt source.

Plissken
3rd July 2012, 05:58
Just wondering, since I'm inheriting from QLineEdit, shouldn't there be some sort of default highlighting that happens even if I override paintEvent? What's happening right now is that it's invisibly highlighting with no default highlight of blue/white. The following code has no effect:


QPalette p = this->palette();
p.setColor( QPalette::Highlight, Qt::blue );
p.setColor( QPalette::HighlightedText, Qt::white );
this->setPalette( p );

ChrisW67
5th July 2012, 07:23
You are replacing the paintEvent from the inherited class. If your paintEvent doesn't paint the selected text in some alternate way, or delegate to something that does, then it won't be painted.

Are you sure that the desired overall effect cannot be more simply achieved with style sheets?