How do I keep a QTextEdit widget from scrolling?



  • I need to place a text edit box of a certain size and I need to constrain the input to just the text that can be fit without scrolling. How do I achieve this?

    I want to prevent this:

    0_1557791269575_0383512d-40e9-4bc8-9f53-f04fec15884e-image.png

    And have the input restricted to two lines that are the lines that fit on this size with this font size, like this:

    0_1557791395169_a0ed8038-0000-431b-af1b-a7fadb18262e-image.png

    So if the user keeps entering text, the box won't allow anything past the last character that fits on the second row

    Any ideas?

    Thanks!


  • Lifetime Qt Champion

    Hi
    Subclass QTextEdit and override keyPressed event.
    There use QFontMetrics fm(this->font);
    to check size of the text. if too big, ignore key
    else add key to the text.

    I assume you want 3 lines if font is smaller so its not always 2 lines ?



  • @mrjj said in How do I keep a QTextEdit widget from scrolling?:

    I assume you want 3 lines if font is smaller so its not always 2 lines ?

    Hello, thanks for helping!

    No, I always want the same font size and only two lines. So, if the user writes enough to reach the end of the second line, then they can't input more text.


  • Lifetime Qt Champion

    @m4l490n
    Hi
    Why not simply use 2 LineEdits then ?
    You can set text maxLength on both and it will give a similar effect.


Log in to reply
 

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