Qt Utilities  6.1.0
Common Qt related C++ classes and routines used by my applications such as dialogs, widgets and models
clearspinbox.cpp
Go to the documentation of this file.
1 #include "./clearspinbox.h"
2 
3 #include <QHBoxLayout>
4 #include <QStyle>
5 #include <QStyleOptionSpinBox>
6 
7 namespace QtUtilities {
8 
20  : QSpinBox(parent)
21  , ButtonOverlay(this, lineEdit())
22  , m_minimumHidden(false)
23 {
25 }
26 
31 {
32 }
33 
37 void ClearSpinBox::handleValueChanged(int value)
38 {
39  updateClearButtonVisibility(value != minimum());
40 }
41 
42 void ClearSpinBox::handleClearButtonClicked()
43 {
44  setValue(minimum());
45 }
46 
47 void ClearSpinBox::handleCustomLayoutCreated()
48 {
49  const QStyle *const s = style();
50  QStyleOptionSpinBox opt;
51  opt.initFrom(this);
52  setContentsMarginsFromEditFieldRectAndFrameWidth(
53  s->subControlRect(QStyle::CC_SpinBox, &opt, QStyle::SC_SpinBoxEditField, this), s->pixelMetric(QStyle::PM_SpinBoxFrameWidth, &opt, this));
54  connect(this, static_cast<void (ClearSpinBox::*)(int)>(&ClearSpinBox::valueChanged), this, &ClearSpinBox::handleValueChanged);
55 }
56 
58 {
59  return value() == minimum();
60 }
61 
62 int ClearSpinBox::valueFromText(const QString &text) const
63 {
64  if (m_minimumHidden && text.isEmpty()) {
65  return minimum();
66  } else {
67  return QSpinBox::valueFromText(text);
68  }
69 }
70 
71 QString ClearSpinBox::textFromValue(int val) const
72 {
73  if (m_minimumHidden && (val == minimum())) {
74  return QString();
75  } else {
76  return QSpinBox::textFromValue(val);
77  }
78 }
79 } // namespace QtUtilities
QtUtilities::ClearSpinBox::isCleared
bool isCleared() const override
Returns whether the related widget is cleared.
Definition: clearspinbox.cpp:57
QtUtilities::ClearSpinBox::~ClearSpinBox
~ClearSpinBox() override
Destroys the clear spin box.
Definition: clearspinbox.cpp:30
clearspinbox.h
QtUtilities::ClearSpinBox::valueFromText
int valueFromText(const QString &text) const override
Definition: clearspinbox.cpp:62
QtUtilities::ButtonOverlay
The ButtonOverlay class is used to display buttons on top of other widgets.
Definition: buttonoverlay.h:25
QtUtilities::ClearSpinBox::textFromValue
QString textFromValue(int val) const override
Definition: clearspinbox.cpp:71
QtUtilities
!
Definition: trylocker.h:8
QtUtilities::ButtonOverlay::ClearSpinBox
friend class ClearSpinBox
Definition: buttonoverlay.h:28
text
#define text
Definition: xmlparsermacros.h:18
QtUtilities::ButtonOverlay::updateClearButtonVisibility
void updateClearButtonVisibility(bool visible)
Updates the visibility of the clear button.
Definition: buttonoverlay.cpp:283
QtUtilities::ButtonOverlay::setClearButtonEnabled
void setClearButtonEnabled(bool enabled)
Sets whether the clear button is enabled.
Definition: buttonoverlay.cpp:125