Lineedit with a clear button

It looks like people have taken a deep liking to having line edits with a clear button. Like the one Aaron made. Since many seem to have missed that post, here’s my take on a QLineEdit with a clear button.

lineedit.h
lineedit.cpp

The code is fairly trivial. So, I won’t bother explaining. The clear button appears only when the line edit has text. Here’s how it looks,

Line edit with no text,
Line edit with no text

Line edit with some text (clear button appears)
Line edit with some text

Line edit with lotsa text (text doesn’t go underneath clear button)
Line edit with lots of text

Update: I got a question asking how to implement the search line edit with a menu, like in thunderbird. The clear button is a QToolButton. Just set a menu :) To move it to the left, just use padding-left and move the clear button appropriately.

twitter