PDA

View Full Version : Clear button in line edit



MTK358
31st July 2010, 01:55
In KDE every line edit has a clear button that appears when there is text.

How do I do that? Preferably something that doesn't require KDE.

Lykurg
31st July 2010, 06:08
It's pure Qt the KDE people use. Just have a look into their sources and see how they make it. Or have a look at wysotas wwWidget (http://www.wysota.eu.org/wwwidgets/). He also provide such a line edit.

In theory it is simply a button which overlays the line edit.

MTK358
31st July 2010, 14:22
I found this, but the links to the source code are broken:

http://labs.trolltech.com/blogs/2007/06/06/lineedit-with-a-clear-button/

MTK358
1st August 2010, 13:27
I figured out how to get a button into QLineEdit:

EnhancedLineEdit::EnhancedLineEdit(QWidget *parent) :
QLineEdit(parent)
{
QHBoxLayout *layout = new QHBoxLayout(this);
setLayout(layout);
layout->addStretch();

QPushButton *clearButton = new QPushButton(this);
layout->addWidget(clearButton);
connect(clearButton, SIGNAL(clicked()), this, SLOT(clear()));

clearButton->setFlat(true);
clearButton->setCursor(QCursor(Qt::ArrowCursor));
clearButton->setIcon(QIcon("/usr/share/oxygen/icons/16x16/actions/edit-clear-locationbar-rtl.png"));
}

Problem is that it's too wide, and for some reason the icon does not show in the button. Also, how do I give it a standard themeable icon, such as perhaps the "Cancel" or "Close Tab" icon?

This also gave me a cool idea: Integrate a "Go" button into the QLineEdit that would emit a returnPressed() signal! That way you don't have to manage two widgets and two signal/slot connections for many QLineEdit applications.