Qt Utilities  6.4.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 
15 static inline auto *getComboBoxLineEdit(QComboBox *comboBox)
16 {
17  comboBox->setEditable(true);
18  return comboBox->lineEdit();
19 }
21 
27  : QComboBox(parent)
28  , ButtonOverlay(this, getComboBoxLineEdit(this))
29 {
31 }
32 
37 {
38 }
39 
43 void ClearComboBox::handleTextChanged(const QString &text)
44 {
46 }
47 
48 void ClearComboBox::handleClearButtonClicked()
49 {
50  clearEditText();
51 }
52 
53 void ClearComboBox::handleCustomLayoutCreated()
54 {
55  const QStyle *const s = style();
56  QStyleOptionComboBox opt;
57  opt.initFrom(this);
58  setContentsMarginsFromEditFieldRectAndFrameWidth(
59  s->subControlRect(QStyle::CC_ComboBox, &opt, QStyle::SC_ComboBoxEditField, this), s->pixelMetric(QStyle::PM_ComboBoxFrameWidth, &opt, this));
60  connect(this, &ClearComboBox::currentTextChanged, this, &ClearComboBox::handleTextChanged);
61 }
62 
64 {
65  return currentText().isEmpty();
66 }
67 } // namespace QtUtilities
The ButtonOverlay class is used to display buttons on top of other widgets.
Definition: buttonoverlay.h:25
void updateClearButtonVisibility(bool visible)
Updates the visibility of the clear button.
void setClearButtonEnabled(bool enabled)
Sets whether the clear button is enabled.
~ClearComboBox() override
Destroys the clear combo box.
bool isCleared() const override
Returns whether the related widget is cleared.
#define text