Qt Utilities: widgets/clearcombobox.cpp Source File

Qt Utilities 6.20.0 Common Qt related C++ classes and routines used by my applications such as dialogs, widgets and models

Loading...
Searching...
No Matches
clearcombobox.cpp
Go to the documentation of this file.
1#include "./clearcombobox.h"
2
3#include <QHBoxLayout>
4#include <QStyle>
5#include <QStyleOptionComboBox>
6
7namespace QtUtilities {
8
13
15static inline auto *getComboBoxLineEdit(QComboBox *comboBox)
16{
17 comboBox->setEditable(true);
18 return comboBox->lineEdit();
19}
21
26 ClearComboBox::ClearComboBox(QWidget *parent)
27 : QComboBox(parent)
28 , ButtonOverlay(this, getComboBoxLineEdit(this))
29{
30 ButtonOverlay::setClearButtonEnabled(true);
31}
32
36ClearComboBox::~ClearComboBox()
37{
38}
39
43void ClearComboBox::handleTextChanged(const QString &text)
44{
45 updateClearButtonVisibility(!text.isEmpty());
46}
47
48void ClearComboBox::handleClearButtonClicked()
49{
50 clearEditText();
51}
52
53void 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
63 bool ClearComboBox::isCleared() const
64{
65 return currentText().isEmpty();
66}
67} // namespace QtUtilities
QtUtilities::ButtonOverlay::updateClearButtonVisibility
void updateClearButtonVisibility(bool visible)
Updates the visibility of the clear button.
Definition buttonoverlay.cpp:283
QtUtilities::ButtonOverlay::ClearComboBox
friend class ClearComboBox
Definition buttonoverlay.h:27
QtUtilities::ButtonOverlay::handleCustomLayoutCreated
virtual void handleCustomLayoutCreated()
Applies additional handling when the button layout has been created.
Definition buttonoverlay.cpp:305
QtUtilities::ButtonOverlay::ButtonOverlay
ButtonOverlay(QWidget *widget)
Constructs a button overlay for the specified widget.
Definition buttonoverlay.cpp:49
QtUtilities::ButtonOverlay::handleClearButtonClicked
virtual void handleClearButtonClicked()
Clears the related widget.
Definition buttonoverlay.cpp:295
QtUtilities::ButtonOverlay::setClearButtonEnabled
void setClearButtonEnabled(bool enabled)
Sets whether the clear button is enabled.
Definition buttonoverlay.cpp:125
QtUtilities::ClearComboBox::~ClearComboBox
~ClearComboBox() override
Destroys the clear combo box.
Definition clearcombobox.cpp:36
QtUtilities::ClearComboBox::isCleared
bool isCleared() const override
Returns whether the related widget is cleared.
Definition clearcombobox.cpp:63
clearcombobox.h
QtUtilities
!
Definition trylocker.h:8
text
#define text
Definition xmlparsermacros.h:18