Custom QLineEdit Selection Highlight



  • I have class inheriting QLineEdit. I'm able to draw text in my overridden paintEvent method, and selection of text works fine, sort of. The highlighting of the selected text is invisible. I can definitely select text by click-dragging the mouse over text, but the visual element of the default blue/white highlight does not work. I assumed the behavior would be there even if overridden. Do I have to write highlight code manually? If so, where do I begin? I've tried using QPalette to set the Qt::Highlight and Qt::HighlightedText colors but there is no effect, only invisible selection.



  • Well, if you reimplemented the paintEvent method, then it is up to you to do the actual painting... That includes text highlights, of course. Could you show us your code for that method? Note that you probably don't want to be doing all rendering on your own. If you look at the sources of QLineEdit, you'll notice that this code is quite complex.



  • @void CustomWidget::paintEvent( QPaintEvent *event )
    {
    QPainter painter( this );
    painter.setRenderHint( QPainter::Antialiasing, true );

    painter.setPen( Qt::red );
    painter.drawLine( 0, size().height() - 2, size().width(), size().height() - 2 );
    
    painter.setPen( Qt::black );
    painter.drawText( 0, size().height() - 4, text() );
    

    }@

    Right now it just has drawing code for the text. Looks like I will have to write my own highlighting code. Thanks, Andre.

    I'm thinking about using setSelection to find what text is selected and from there figure out where to draw a blue square and what text to highlight white.



  • What is the reason you want to do your own drawing? Would it not be easier to use the rendering already done by QLineEdit by default?



  • I'm trying to see if I can replicate in Qt what's a called a character sheet used in pen-and-paper roleplaying games (example image: !http://i56.tinypic.com/24cef46.jpg(example)!). So the look and feel of the widget needs to be the same on all platforms, and not look like a default QLineEdit used in business or office type applications. The idea is that users will be able to create their own character sheets by using custom widgets from a toolbox. This is just one part of a larger application I've designed.



  • Seems to me you can create that custom look using a stylesheet as well, wich much less effort.



  • Thanks. I will check out the documentation for style sheets.



  • I've created a stylesheet to create black line (actually, just the bottom border visible) for text. However, there is a gap between the baseline of the characters and the line itself. Is there anyway to shift the characters to rest right on the line?

    Edit: I have moved your other question to a "separate topic":/forums/viewthread/18550/ because it is on a completely different issue; Andre



  • [quote author="Plissken" date="1341420919"]I've created a stylesheet to create black line (actually, just the bottom border visible) for text. However, there is a gap between the baseline of the characters and the line itself. Is there anyway to shift the characters to rest right on the line?[/quote]

    Sure there is: try playing around with the padding. You can try setting the bottom padding to a negative value.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.