Skip to content

Commit 2954170

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 error_locations to only be populated for validators that support it - Fix setEnabled to clear focus before disabling to ensure proper cleanup of invalid visual state
1 parent 271fd20 commit 2954170

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

src/qt/qvalidatedlineedit.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,13 @@ void QValidatedLineEdit::setValid(bool _valid, bool with_warning, const std::vec
9292

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

98103
QLineEdit::focusInEvent(evt);
99104
}
@@ -108,7 +113,12 @@ void QValidatedLineEdit::focusOutEvent(QFocusEvent *evt)
108113
void QValidatedLineEdit::markValid()
109114
{
110115
// As long as a user is typing ensure we display state as valid
111-
setValid(true);
116+
// unless we're validating while editing
117+
if (m_allow_validation_while_editing) {
118+
checkValidity();
119+
} else {
120+
setValid(true);
121+
}
112122
}
113123

114124
void QValidatedLineEdit::clear()
@@ -122,7 +132,11 @@ void QValidatedLineEdit::setEnabled(bool enabled)
122132
if (!enabled)
123133
{
124134
// A disabled QValidatedLineEdit should be marked valid
135+
// Clear focus first to trigger focusOutEvent
136+
// required to properly clear invalid visual state
137+
if (hasFocus()) { clearFocus(); }
125138
setValid(true);
139+
setStyleSheet("");
126140
}
127141
else
128142
{
@@ -138,7 +152,7 @@ void QValidatedLineEdit::checkValidity()
138152
const bool has_warning = checkWarning();
139153
if (text().isEmpty())
140154
{
141-
setValid(true);
155+
setValid(m_allow_empty_input);
142156
}
143157
else if (hasAcceptableInput())
144158
{
@@ -156,7 +170,7 @@ void QValidatedLineEdit::checkValidity()
156170
} else {
157171
int pos = 0;
158172
validation_result = checkValidator->validate(address, pos);
159-
error_locations.push_back(pos);
173+
// do not provide error locations for validators that do not support it
160174
}
161175
if (validation_result == QValidator::Acceptable)
162176
setValid(true, has_warning);

src/qt/qvalidatedlineedit.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class QValidatedLineEdit : public QLineEdit
2222
bool isValid();
2323
void setWarningValidator(const QValidator *);
2424
bool hasWarning() const;
25+
void setAllowEmptyInput(bool allow) { m_allow_empty_input = allow; }
26+
void setAllowValidationWhileEditing(bool allow) { m_allow_validation_while_editing = allow; }
2527

2628
protected:
2729
void focusInEvent(QFocusEvent *evt) override;
@@ -32,6 +34,8 @@ class QValidatedLineEdit : public QLineEdit
3234
const QValidator* checkValidator{nullptr};
3335
bool m_has_warning{false};
3436
const QValidator *m_warning_validator{nullptr};
37+
bool m_allow_empty_input{true};
38+
bool m_allow_validation_while_editing{false};
3539

3640
public Q_SLOTS:
3741
void setText(const QString&);

0 commit comments

Comments
 (0)