qtutilities/widgets/clearlineedit.cpp

58 lines
1.3 KiB
C++
Raw Permalink Normal View History

2015-09-06 20:19:21 +02:00
#include "./clearlineedit.h"
2015-04-22 18:57:44 +02:00
#include <QStyle>
#include <QStyleOptionFrame>
namespace QtUtilities {
2015-04-22 18:57:44 +02:00
/*!
* \class ClearLineEdit
2015-04-22 18:57:44 +02:00
* \brief A QLineEdit with an embedded button for clearing its contents.
*/
/*!
* \brief Constructs a clear line edit.
*/
2017-05-01 03:16:25 +02:00
ClearLineEdit::ClearLineEdit(QWidget *parent)
: QLineEdit(parent)
, ButtonOverlay(this, this)
2015-04-22 18:57:44 +02:00
{
ButtonOverlay::setClearButtonEnabled(true);
}
/*!
* \brief Destroys the clear combo box.
*/
ClearLineEdit::~ClearLineEdit()
2017-05-01 03:16:25 +02:00
{
}
2015-04-22 18:57:44 +02:00
/*!
* \brief Updates the visibility of the clear button.
*/
void ClearLineEdit::handleTextChanged(const QString &text)
{
updateClearButtonVisibility(!text.isEmpty());
}
void ClearLineEdit::handleClearButtonClicked()
{
clear();
}
void ClearLineEdit::handleCustomLayoutCreated()
{
const QStyle *const s = style();
QStyleOptionFrame opt;
opt.initFrom(this);
setContentsMarginsFromEditFieldRectAndFrameWidth(s->subElementRect(QStyle::SE_LineEditContents, &opt, this),
s->pixelMetric(QStyle::PM_DefaultFrameWidth, &opt, m_widget), s->pixelMetric(QStyle::PM_LayoutVerticalSpacing, &opt, m_widget));
connect(this, &ClearLineEdit::textChanged, this, &ClearLineEdit::handleTextChanged);
}
2015-04-22 18:57:44 +02:00
bool ClearLineEdit::isCleared() const
{
return text().isEmpty();
}
} // namespace QtUtilities