Qt Utilities  5.6.0
Common Qt related C++ classes and routines used by my applications such as dialogs, widgets and models
clearcombobox.cpp
Go to the documentation of this file.
1 #include "./clearcombobox.h"
2 
3 #include <QHBoxLayout>
4 #include <QStyle>
5 #include <QStyleOptionComboBox>
6 
7 namespace Widgets {
8 
17 ClearComboBox::ClearComboBox(QWidget *parent) :
18  QComboBox(parent),
19  ButtonOverlay(this)
20 {
21  const QMargins margins = contentsMargins();
22  QStyleOptionComboBox opt;
23  opt.initFrom(this);
24  const int frameWidth = style()->pixelMetric(QStyle::PM_ComboBoxFrameWidth, &opt, this);
25  const int pad = 2;
26  const int buttonWidth = style()->subControlRect(QStyle::CC_ComboBox, &opt, QStyle::SC_ComboBoxArrow, this).width();
27  buttonLayout()->setContentsMargins(margins.left() + frameWidth + pad, margins.top() + frameWidth, margins.right() + frameWidth + pad + buttonWidth, margins.bottom() + frameWidth);
28  setClearButtonEnabled(isEditable());
29  connect(this, &ClearComboBox::currentTextChanged, this, &ClearComboBox::handleTextChanged);
30 }
31 
36 {}
37 
41 void ClearComboBox::handleTextChanged(const QString &text)
42 {
43  updateClearButtonVisibility(!text.isEmpty());
44 }
45 
46 void ClearComboBox::handleClearButtonClicked()
47 {
48  clearEditText();
49 }
50 
52 {
53  return currentText().isEmpty();
54 }
55 
56 }
void updateClearButtonVisibility(bool visible)
Updates the visibility of the clear button.
bool isCleared() const
Returns whether the related widget is cleared.
QHBoxLayout * buttonLayout()
Returns the layout manager holding the buttons.
Definition: buttonoverlay.h:51
ClearComboBox(QWidget *parent=nullptr)
Constructs a clear combo box.
~ClearComboBox()
Destroys the clear combo box.
The ButtonOverlay class is used to display buttons on top of other widgets.
Definition: buttonoverlay.h:17
void setClearButtonEnabled(bool enabled)
Sets whether the clear button is enabled.
Provides a set of extended widgets such as ClearLineEdit and ClearComboBox.
Definition: buttonoverlay.h:13
#define text