2013-10-23 02:52:35 +00:00
|
|
|
#include "lineedit.hpp"
|
2013-02-14 10:20:47 +00:00
|
|
|
|
2013-10-23 02:52:35 +00:00
|
|
|
LineEdit::LineEdit(QWidget* parent)
|
2013-02-14 10:20:47 +00:00
|
|
|
: QLineEdit(parent)
|
2013-10-23 02:52:35 +00:00
|
|
|
{
|
|
|
|
setupClearButton();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LineEdit::setupClearButton()
|
2013-02-14 10:20:47 +00:00
|
|
|
{
|
|
|
|
mClearButton = new QToolButton(this);
|
2023-01-06 19:01:50 +00:00
|
|
|
mClearButton->setIcon(QIcon::fromTheme("edit-clear"));
|
2013-02-14 10:20:47 +00:00
|
|
|
mClearButton->setCursor(Qt::ArrowCursor);
|
|
|
|
mClearButton->setStyleSheet("QToolButton { border: none; padding: 0px; }");
|
|
|
|
mClearButton->hide();
|
2022-08-23 17:35:54 +00:00
|
|
|
connect(mClearButton, &QToolButton::clicked, this, &LineEdit::clear);
|
|
|
|
connect(this, &LineEdit::textChanged, this, &LineEdit::updateClearButton);
|
2013-02-14 10:20:47 +00:00
|
|
|
}
|
|
|
|
|
2013-10-23 02:52:35 +00:00
|
|
|
void LineEdit::resizeEvent(QResizeEvent*)
|
2013-02-14 10:20:47 +00:00
|
|
|
{
|
|
|
|
QSize sz = mClearButton->sizeHint();
|
|
|
|
int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
|
|
|
|
mClearButton->move(rect().right() - frameWidth - sz.width(), (rect().bottom() + 1 - sz.height()) / 2);
|
|
|
|
}
|
|
|
|
|
2013-10-23 02:52:35 +00:00
|
|
|
void LineEdit::updateClearButton(const QString& text)
|
2013-02-14 10:20:47 +00:00
|
|
|
{
|
|
|
|
mClearButton->setVisible(!text.isEmpty());
|
|
|
|
}
|