Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom input validation for LineEdit/TextEdit #5597

Closed
etiennep249 opened this issue Jul 10, 2024 · 3 comments
Closed

Custom input validation for LineEdit/TextEdit #5597

etiennep249 opened this issue Jul 10, 2024 · 3 comments

Comments

@etiennep249
Copy link

etiennep249 commented Jul 10, 2024

Currently, the only way to do input validation on LineEdits and TextEdits is to set the input-type property, but it's pretty limited.

I can think of three different ways to handle this:

  • A callback: validate(string, int) -> bool which would take the character added (or string pasted) and the position where it's being added and return true if it should be accepted, false otherwise. It would need to be called every time a character is entered (after input-type validation?) and when a string is pasted in.
  • A regex field that would be matched against every time a character is entered or a string is pasted. If the match fails, the change is refused.
  • The existing edited(string) callback could be called before the change instead of after, and take a boolean return value which would dictate whether or not the change is accepted. This is probably the least preferable one, since there would be no easy way to know what changed, and where.

Some use cases for this: A field that only allows numbers in a specific base. A field with a maximum length. Preventing the use of specific characters, such as non-ascii.

This would allow input validation directly when editing the LineEdit rather than when pressing some accept button later, resulting in a better user experience.

If time is an issue, I can contribute it, just let me know if/how you want it done.

@tronical
Copy link
Member

@hunger is this what you also had in mind with #4288 ?

@hunger
Copy link
Member

hunger commented Jul 15, 2024

Yes, this is definitely related.

I'd suggest to take the entire string and the cursor position though in that validate call. We should also provide some standard validators (int, float, regexp, etc.).

Qt's approach is documented here: https://doc.qt.io/qt-6/qvalidator.html

They have a way to get to the locale and a way to optionally fixup the input if it is not valid. Both seem like good features to me.

@ogoffart
Copy link
Member

I'm going to close this issue as Duplicate of #4288
I think a validate(string)->bool that pass the full string. But it might be a bit more complicated that that since we might want to allow "incomplete" input. Example, if i want to enter a date, "12/07/" should be allowed as you're typing the date.

@ogoffart ogoffart closed this as not planned Won't fix, can't repro, duplicate, stale Jul 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants