Skip to content

Commit 2314eb0

Browse files
committed
GUI/QValidatedLineEdit: add validation control features and fix error highlighting
- Add setAllowEmptyInput() to control whether empty input is valid - Add setAllowValidationWhileEditing() for real-time validation - Fix focusInEvent to handle validation-while-editing fields correctly - Fix setEnabled to clear focus before disabling to ensure proper cleanup of invalid visual state - Add changeEvent() handler to update validation colors on palette/theme changes
1 parent 271fd20 commit 2314eb0

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

src/qt/qvalidatedlineedit.cpp

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@
1010

1111
#include <QColor>
1212
#include <QCoreApplication>
13+
#include <QEvent>
14+
#include <QFocusEvent>
1315
#include <QFont>
1416
#include <QInputMethodEvent>
1517
#include <QList>
1618
#include <QTextCharFormat>
19+
#include <QValidator>
1720

1821
QValidatedLineEdit::QValidatedLineEdit(QWidget* parent)
1922
: QLineEdit(parent)
@@ -92,8 +95,13 @@ void QValidatedLineEdit::setValid(bool _valid, bool with_warning, const std::vec
9295

9396
void QValidatedLineEdit::focusInEvent(QFocusEvent *evt)
9497
{
95-
// Clear invalid flag on focus
96-
setValid(true);
98+
if (!m_allow_validation_while_editing) {
99+
// Clear invalid flag on focus for normal fields
100+
setValid(true);
101+
} else {
102+
// For validation-while-editing fields, recheck validity
103+
checkValidity();
104+
}
97105

98106
QLineEdit::focusInEvent(evt);
99107
}
@@ -105,10 +113,27 @@ void QValidatedLineEdit::focusOutEvent(QFocusEvent *evt)
105113
QLineEdit::focusOutEvent(evt);
106114
}
107115

116+
void QValidatedLineEdit::changeEvent(QEvent *e)
117+
{
118+
if (e->type() == QEvent::PaletteChange) {
119+
if (!valid) {
120+
// Refresh styling using setValid function
121+
setValid(valid);
122+
}
123+
}
124+
125+
QLineEdit::changeEvent(e);
126+
}
127+
108128
void QValidatedLineEdit::markValid()
109129
{
110130
// As long as a user is typing ensure we display state as valid
111-
setValid(true);
131+
// unless we're validating while editing
132+
if (m_allow_validation_while_editing) {
133+
checkValidity();
134+
} else {
135+
setValid(true);
136+
}
112137
}
113138

114139
void QValidatedLineEdit::clear()
@@ -122,6 +147,9 @@ void QValidatedLineEdit::setEnabled(bool enabled)
122147
if (!enabled)
123148
{
124149
// A disabled QValidatedLineEdit should be marked valid
150+
// Clear focus first to trigger focusOutEvent
151+
// required to properly clear invalid visual state
152+
if (hasFocus()) { clearFocus(); }
125153
setValid(true);
126154
}
127155
else
@@ -138,7 +166,7 @@ void QValidatedLineEdit::checkValidity()
138166
const bool has_warning = checkWarning();
139167
if (text().isEmpty())
140168
{
141-
setValid(true);
169+
setValid(m_allow_empty_input);
142170
}
143171
else if (hasAcceptableInput())
144172
{

src/qt/qvalidatedlineedit.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77

88
#include <QLineEdit>
99

10+
QT_BEGIN_NAMESPACE
11+
class QEvent;
12+
class QFocusEvent;
13+
class QValidator;
14+
QT_END_NAMESPACE
15+
1016
/** Line edit that can be marked as "invalid" to show input validation feedback. When marked as invalid,
1117
it will get a red background until it is focused.
1218
*/
@@ -22,16 +28,21 @@ class QValidatedLineEdit : public QLineEdit
2228
bool isValid();
2329
void setWarningValidator(const QValidator *);
2430
bool hasWarning() const;
31+
void setAllowEmptyInput(bool allow) { m_allow_empty_input = allow; }
32+
void setAllowValidationWhileEditing(bool allow) { m_allow_validation_while_editing = allow; }
2533

2634
protected:
2735
void focusInEvent(QFocusEvent *evt) override;
2836
void focusOutEvent(QFocusEvent *evt) override;
37+
void changeEvent(QEvent *e) override;
2938

3039
private:
3140
bool valid{true};
3241
const QValidator* checkValidator{nullptr};
3342
bool m_has_warning{false};
3443
const QValidator *m_warning_validator{nullptr};
44+
bool m_allow_empty_input{true};
45+
bool m_allow_validation_while_editing{false};
3546

3647
public Q_SLOTS:
3748
void setText(const QString&);

0 commit comments

Comments
 (0)