Qt Utilities 6.18.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
20 : QSpinBox(parent)
21 , ButtonOverlay(this, lineEdit())
22 , m_minimumHidden(false)
23{
25}
26
33
37void ClearSpinBox::handleValueChanged(int value)
38{
39 updateClearButtonVisibility(value != minimum());
40}
41
43{
44 setValue(minimum());
45}
46
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
62int 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
void updateClearButtonVisibility(bool visible)
Updates the visibility of the clear button.
virtual void handleCustomLayoutCreated()
Applies additional handling when the button layout has been created.
ButtonOverlay(QWidget *widget)
Constructs a button overlay for the specified widget.
virtual void handleClearButtonClicked()
Clears the related widget.
void setClearButtonEnabled(bool enabled)
Sets whether the clear button is enabled.
bool isCleared() const override
Returns whether the related widget is cleared.
QString textFromValue(int val) const override
ClearSpinBox(QWidget *parent=nullptr)
Constructs a clear spin box.
~ClearSpinBox() override
Destroys the clear spin box.
int valueFromText(const QString &text) const override
#define text