Skip to content

Commit 52af5bf

Browse files
committed
GUI/QValidatedLineEdit: add validation control features and fix error highlighting
- Added setAllowEmptyInput() to control whether empty input is considered valid - Added setAllowValidationWhileEditing() to enable real-time validation while typing - Fixed error_locations handling to only apply character-level formatting for validators that support it (BitcoinAddressEntryValidator), preventing incorrect highlighting of first character for standard validators - When validation while editing is enabled, markValid() now triggers checkValidity() instead of forcing valid state
1 parent 271fd20 commit 52af5bf

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/qt/qvalidatedlineedit.cpp

Lines changed: 15 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()
@@ -138,7 +148,7 @@ void QValidatedLineEdit::checkValidity()
138148
const bool has_warning = checkWarning();
139149
if (text().isEmpty())
140150
{
141-
setValid(true);
151+
setValid(m_allow_empty_input);
142152
}
143153
else if (hasAcceptableInput())
144154
{
@@ -156,7 +166,7 @@ void QValidatedLineEdit::checkValidity()
156166
} else {
157167
int pos = 0;
158168
validation_result = checkValidator->validate(address, pos);
159-
error_locations.push_back(pos);
169+
// do not provide error locations for validators that do not support it
160170
}
161171
if (validation_result == QValidator::Acceptable)
162172
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)