Qt Utilities: widgets/clearspinbox.cpp Source File

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

Loading...
Searching...
No Matches
clearspinbox.cpp
Go to the documentation of this file.
1#include "./clearspinbox.h"
2
3#include <QHBoxLayout>
4#include <QStyle>
5#include <QStyleOptionSpinBox>
6
7namespace QtUtilities {
8
15
19 ClearSpinBox::ClearSpinBox(QWidget *parent)
20 : QSpinBox(parent)
21 , ButtonOverlay(this, lineEdit())
22 , m_minimumHidden(false)
23{
24 ButtonOverlay::setClearButtonEnabled(true);
25}
26
30ClearSpinBox::~ClearSpinBox()
31{
32}
33
37void ClearSpinBox::handleValueChanged(int value)
38{
39 updateClearButtonVisibility(value != minimum());
40}
41
42void ClearSpinBox::handleClearButtonClicked()
43{
44 setValue(minimum());
45}
46
47void 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
57 bool ClearSpinBox::isCleared() const
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
71QString 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::ButtonOverlay::updateClearButtonVisibility
void updateClearButtonVisibility(bool visible)
Updates the visibility of the clear button.
Definition buttonoverlay.cpp:283
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::ButtonOverlay::ClearSpinBox
friend class ClearSpinBox
Definition buttonoverlay.h:28
QtUtilities::ClearSpinBox::isCleared
bool isCleared() const override
Returns whether the related widget is cleared.
Definition clearspinbox.cpp:57
QtUtilities::ClearSpinBox::textFromValue
QString textFromValue(int val) const override
Definition clearspinbox.cpp:71
QtUtilities::ClearSpinBox::ClearSpinBox
ClearSpinBox(QWidget *parent=nullptr)
Constructs a clear spin box.
Definition clearspinbox.cpp:19
QtUtilities::ClearSpinBox::~ClearSpinBox
~ClearSpinBox() override
Destroys the clear spin box.
Definition clearspinbox.cpp:30
QtUtilities::ClearSpinBox::valueFromText
int valueFromText(const QString &text) const override
Definition clearspinbox.cpp:62
clearspinbox.h
QtUtilities
!
Definition trylocker.h:8
text
#define text
Definition xmlparsermacros.h:18