qtutilities/widgets/clearcombobox.cpp

58 lines
1.5 KiB
C++
Raw Normal View History

2015-09-06 20:19:21 +02:00
#include "./clearcombobox.h"
2015-04-22 18:57:44 +02:00
#include <QHBoxLayout>
#include <QStyle>
#include <QStyleOptionComboBox>
namespace Widgets {
/*!
* \class Widgets::ClearComboBox
* \brief A QComboBox with an embedded button for clearing its contents.
*/
/*!
* \brief Constructs a clear combo box.
*/
2017-05-01 03:16:25 +02:00
ClearComboBox::ClearComboBox(QWidget *parent)
: QComboBox(parent)
, ButtonOverlay(this)
2015-04-22 18:57:44 +02:00
{
const QMargins margins = contentsMargins();
QStyleOptionComboBox opt;
opt.initFrom(this);
const int frameWidth = style()->pixelMetric(QStyle::PM_ComboBoxFrameWidth, &opt, this);
const int pad = 2;
const int buttonWidth = style()->subControlRect(QStyle::CC_ComboBox, &opt, QStyle::SC_ComboBoxArrow, this).width();
2017-05-01 03:16:25 +02:00
buttonLayout()->setContentsMargins(margins.left() + frameWidth + pad, margins.top() + frameWidth,
margins.right() + frameWidth + pad + buttonWidth, margins.bottom() + frameWidth);
2015-04-22 18:57:44 +02:00
setClearButtonEnabled(isEditable());
connect(this, &ClearComboBox::currentTextChanged, this, &ClearComboBox::handleTextChanged);
}
/*!
* \brief Destroys the clear combo box.
*/
ClearComboBox::~ClearComboBox()
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 ClearComboBox::handleTextChanged(const QString &text)
{
updateClearButtonVisibility(!text.isEmpty());
}
void ClearComboBox::handleClearButtonClicked()
{
clearEditText();
}
bool ClearComboBox::isCleared() const
{
return currentText().isEmpty();
}
} // namespace Widgets