Clearing a QLineEdit

3 views
Skip to first unread message

Steven Sroka

unread,
Jun 20, 2011, 12:14:01 AM6/20/11
to kde-...@kde.org
Hi everyone,

Does anyone know how to clear the contents of a QLineEdit when a user
sets focus to it (ie. clicks it, presses the tab key repeatedly,
etc.)?

>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<

Parker Coates

unread,
Jun 20, 2011, 1:41:38 AM6/20/11
to kde-...@kde.org
On Mon, Jun 20, 2011 at 00:14, Steven Sroka wrote:
> Hi everyone,
>
> Does anyone know how to clear the contents of a QLineEdit when a user
> sets focus to it (ie. clicks it, presses the tab key repeatedly,
> etc.)?

Subclass KLineEdit (which you`re using instead of QLineWidget, right?)
and reimplement focusInEvent() to call clear().

But why are you trying to do this? In general it seems like a bad
idea. If it`s to implement some kind of instructional message, see
QLineEdit::setPlaceholderText() or KLineEdit::setClickMessage().

Parker

Thomas Lübking

unread,
Jun 20, 2011, 4:52:39 PM6/20/11
to kde-...@kde.org
Am Monday 20 June 2011 schrieb Steven Sroka:
> Does anyone know how to clear the contents of a QLineEdit when a user
> sets focus to it (ie. clicks it, presses the tab key repeatedly,
> etc.)?

You don't have to subclass it, just connect some slot to
void QApplication::focusChanged ( QWidget * old, QWidget * now )
Check if "new" is you lineedit and do whatever you must.

I however agree that clearing is likely a bad idea (assume you just wanted to
fix a one char typo) - you might want to selectAll() instead?


Cheers,
Thomas

Steven Sroka

unread,
Jun 20, 2011, 5:44:41 PM6/20/11
to kde-...@kde.org
>On 20 June 2011 01:41, Parker Coates <parker...@kdemail.net> wrote:
> On Mon, Jun 20, 2011 at 00:14, Steven Sroka wrote:
>> Hi everyone,
>>
>> Does anyone know how to clear the contents of a QLineEdit when a user
>> sets focus to it (ie. clicks it, presses the tab key repeatedly,
>> etc.)?
>
> Subclass KLineEdit (which you`re using instead of QLineWidget, right?)
> and reimplement focusInEvent() to call clear().
>
> But why are you trying to do this? In general it seems like a bad
> idea. If it`s to implement some kind of instructional message, see
> QLineEdit::setPlaceholderText() or KLineEdit::setClickMessage().

This is exactly what I need. Thank you.

Do you know how to centre the ClickMessage text? If I set the
alignment of the text, only the text that the user types in is aligned
and not the ClickMessage text.

Michael Pyne

unread,
Jun 20, 2011, 6:11:58 PM6/20/11
to kde-...@kde.org
On Monday, June 20, 2011 17:44:41 Steven Sroka wrote:
> Do you know how to centre the ClickMessage text? If I set the
> alignment of the text, only the text that the user types in is aligned
> and not the ClickMessage text.

Strictly speaking I think what you want to do is more properly part of the
style, and not part of the application itself. ;)

i.e. it is not centered because the user's style doesn't center placeholder
text.

Regards,
- Michael Pyne

signature.asc
Reply all
Reply to author
Forward
0 new messages