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. Custom QLineEdit Selection Highlight
Forum Updated to NodeBB v4.3 + New Features

Custom QLineEdit Selection Highlight

Scheduled Pinned Locked Moved General and Desktop
9 Posts 2 Posters 6.3k Views 1 Watching
  • 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.
  • P Offline
    P Offline
    Plissken
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Plissken
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          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?

          1 Reply Last reply
          0
          • P Offline
            P Offline
            Plissken
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • P Offline
                P Offline
                Plissken
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  Plissken
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #9

                    [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.

                    1 Reply Last reply
                    0

                    • Login

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