Lineedit with a clear button

Posted by Girish Ramakrishnan on June 6, 2007 · 13 comments

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.

QShare(this)

Possibly related posts:

  1. Resolving QtScript’s “Legacy” APIs

13 comments

1 Louis June 6, 2007 at 6:32 pm
 

Just curious if that clear button is configurable. That button is a “close” button, and may be confusing or intimidating (“..is that gonna close my text box?!”).

2 Steven Fisher June 6, 2007 at 6:32 pm
 

Excellent. I think I can make a Mac OS X search field-like line edit using this technique. I was never happy with the CSS method. Thanks!

3 Bram Schoenmakers June 6, 2007 at 6:42 pm
 

Does it have a threshold for making it really activate (like close buttons on tabs have)? Otherwise I think I accidentally will press that button whenever I want to select some text.

4 Guid June 6, 2007 at 9:50 pm
 

Terrific! I was searching for something like that!
CSS integration in Qt4 widgets is really great \o/

5 Henrik Pauli June 7, 2007 at 6:15 am
 

Nice, but I’m seriously concerned about accidental clicks on that button, especially once the text reaches it (say, you mean to select and later replace something from the end of the string). — it is pretty much my problem with the close buttons inside tabs that seem to be the newest fad in both Firefox, and also (especially as hover-activated) in KDE’s several applications (Konqueror, Kate). I can’t count the amount of times I accidentally closed something instead of just selecting it.

Okay, I just realised Bram is touching this issue as well: Bram, is that threshold a KDE4 thing? I surely never saw any in KDE3 :)

I also think that Aaron’s use of the “backspace” icon is probably a better choice than to have a generic X button :)

6 girish June 7, 2007 at 7:30 am
 

Steven Fisher: You are going to be disappointed to learn that I achieved this using Style Sheets. If you don’t like this, the only way to do it is by creating a custom QStyle and implementing SE_LineEditContents and requires some trickery to make the line edit have native borders.

7 girish June 7, 2007 at 7:31 am
 

Bram, Henrik: Valid point that the user might accidently click the clear button. As Bram suggested, we can add a threshold. Or we can have additional padding on the right. Or we can move out the clear button _outside_ the line edit. Here try this (instead of padding-right in lineedit.cpp),
setStyleSheet(QString(“QLineEdit { margin-right: %1px; } “).arg(clearButton->sizeHint().width() + frameWidth + 1));

Of course, you will probably need to toggle the clear button’s enable state instead of visibility. In any case, the point of the example was to help people get started with something like that since I hear this request time and again. Feel free to send me patches for your ideas :)

Henrik: I dislike the X button too. Its what “locate close*png” found :)

8 Frinring June 7, 2007 at 1:17 pm
 

While you are at it:
What I would like even more are some indicators that there is text hidden outside the displayed one, perhaps by using some arrows, shadows, greying out, or something else. Clicking the arrows would scroll the text a little. I miss this not only with the lineedit, but all other widgets where content might be outside of the “viewport”. Scrollbars are only second best to help. E.g. with the lineedit they would be overkill.

9 Will June 7, 2007 at 3:22 pm
 

Now if only there was an easy way to make the line edit have rounded corners to provide a truly native search box look and feel.

10 Harald Fernengel June 8, 2007 at 3:49 pm
 

Please no timeout or other magic behavior, a simple ALT-Z undoes the accidental clear.

11 berkus June 8, 2007 at 8:02 pm
 

Harald, maybe add an “Undo clear” button instead of clear button once the lineedit is cleared? :)

12 Gnud June 14, 2007 at 10:46 pm
 

Girish:
What, that is the best you found? :P
Have a look at the standard unix desktop icon set Tango:

13 Gnud June 14, 2007 at 10:47 pm
 

Hmm- your comment didnt like img-tags.
http://tango.freedesktop.org/static/cvs/tango-icon-theme/22×22/actions/edit-clear.png

Comments on this entry are closed.

Previous post:

Next post: