Skip to content

Commit 6edafad

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 6edafad

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/qt/qvalidatedlineedit.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,10 @@ 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+
// Clear invalid flag on focus, unless we're validating while editing
96+
if (!m_allow_validation_while_editing) {
97+
setValid(true);
98+
}
9799

98100
QLineEdit::focusInEvent(evt);
99101
}
@@ -108,7 +110,12 @@ void QValidatedLineEdit::focusOutEvent(QFocusEvent *evt)
108110
void QValidatedLineEdit::markValid()
109111
{
110112
// As long as a user is typing ensure we display state as valid
111-
setValid(true);
113+
// unless we're validating while editing
114+
if (m_allow_validation_while_editing) {
115+
checkValidity();
116+
} else {
117+
setValid(true);
118+
}
112119
}
113120

114121
void QValidatedLineEdit::clear()
@@ -138,7 +145,7 @@ void QValidatedLineEdit::checkValidity()
138145
const bool has_warning = checkWarning();
139146
if (text().isEmpty())
140147
{
141-
setValid(true);
148+
setValid(m_allow_empty_input);
142149
}
143150
else if (hasAcceptableInput())
144151
{
@@ -156,7 +163,7 @@ void QValidatedLineEdit::checkValidity()
156163
} else {
157164
int pos = 0;
158165
validation_result = checkValidator->validate(address, pos);
159-
error_locations.push_back(pos);
166+
// do not provide error locations for validators that do not support it
160167
}
161168
if (validation_result == QValidator::Acceptable)
162169
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)