Qt Utilities  6.0.1
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 QtUtilities {
8 
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,
28  margins.right() + frameWidth + pad + buttonWidth, margins.bottom() + frameWidth);
29  setClearButtonEnabled(isEditable());
30  connect(this, &ClearComboBox::currentTextChanged, this, &ClearComboBox::handleTextChanged);
31 }
32 
37 {
38 }
39 
43 void ClearComboBox::handleTextChanged(const QString &text)
44 {
46 }
47 
48 void ClearComboBox::handleClearButtonClicked()
49 {
50  clearEditText();
51 }
52 
54 {
55  return currentText().isEmpty();
56 }
57 } // namespace QtUtilities
QtUtilities::ClearComboBox::ClearComboBox
ClearComboBox(QWidget *parent=nullptr)
Constructs a clear combo box.
Definition: clearcombobox.cpp:17
clearcombobox.h
QtUtilities::ButtonOverlay
The ButtonOverlay class is used to display buttons on top of other widgets.
Definition: buttonoverlay.h:17
QtUtilities::ClearComboBox::isCleared
bool isCleared() const override
Returns whether the related widget is cleared.
Definition: clearcombobox.cpp:53
QtUtilities::ClearComboBox::~ClearComboBox
~ClearComboBox() override
Destroys the clear combo box.
Definition: clearcombobox.cpp:36
QtUtilities
!
Definition: trylocker.h:8
QtUtilities::ButtonOverlay::buttonLayout
QHBoxLayout * buttonLayout()
Returns the layout manager holding the buttons.
Definition: buttonoverlay.h:50
text
#define text
Definition: xmlparsermacros.h:18
QtUtilities::ButtonOverlay::updateClearButtonVisibility
void updateClearButtonVisibility(bool visible)
Updates the visibility of the clear button.
Definition: buttonoverlay.cpp:153
QtUtilities::ButtonOverlay::setClearButtonEnabled
void setClearButtonEnabled(bool enabled)
Sets whether the clear button is enabled.
Definition: buttonoverlay.cpp:62