Nominate our 2022 Qt Champions!

Drawing the text as QLineEdit+InputMask does.

  • Lifetime Qt Champion

    I have a delegate that
    creates a LineEdit and sets an inputmask on it.

    This is purely for presentation and this format is not saved to the model.

    However, im looking for some drawText
    function to draw it like the LineEdit does so it can look the same
    in/out of editing the item.

    Even its easy to mimic manually, i was wondering if there was some function for it that i
    keep not seeing looking around in the source. :)

  • Moderators

    AFAIK there's no single method that does it. At least Qt itself doesn't seem to be using anything like that internally.
    I'd just mimic what QLineEdit does internally in its paintEvent(). If you don't go wild with customization you can get away with couple calls to QStyle and QPainter::drawText.

  • Lifetime Qt Champion


    ok so seems there is no dedicated function as such.
    thx for confirming. :)

    Can you elaborate on what QStyle calls you are thinking of?
    Only using QPainter currently so im curious.

  • Moderators

    To draw the control you could use two QStyle::drawPrimitive calls. One with QStyle::PE_PanelLineEdit element to draw the background and QStyle::PE_FrameLineEdit to draw the frame.
    You can then use QStyle::subElementRect() with QStyle::SE_LineEditContents param to get the exact rectangle for the text.

    QLineEdit uses QTextLayout internally to draw the text, but if you don't do any formatting, alignment or other shenanigans simple QPainter::drawText should be fine.

  • Lifetime Qt Champion

    Ah, for the exact look.
    I can see how my question sounds like that in retro reading.
    It was only the text part. not rest of the LineWidget but
    QStyle::subElementRect() is a good idea as i have a slight pixel shift and
    it makes it look better when open/close editor.

    Well no, shenanigans :)
    Its just Feet|Inch|1/16 inch

    So drawText it is.

    Thank you

Log in to reply