[prev in list] [next in list] [prev in thread] [next in thread]
List: pykde
Subject: Re: QValidator.State TypeError
From: Ognyan Moore <ognyan.moore () gmail ! com>
Date: 2023-06-14 15:45:02
Message-ID: CA+nduTHM+hhUfPe5nbQrYi-0egQ+mEp6xKiXN5H124PGcmu9jg () mail ! gmail ! com
[Download RAW message or body]
Sure enough, that's working; I should get in the habit of calling help, not
sure why that never crossed my mind.
Sorry for the noise,
Thanks Florian.
On Wed, Jun 14, 2023 at 8:37 AM Florian Bruhin <me@the-compiler.org> wrote:
> Hey,
>
> > When implementing my own subclass of QValidator, I get a "TypeError:
> > invalid result from Validator.validate()" error followed by a segfault on
> > macOS for the following code:
> >
> > Example:
> >
> > [...]
> >
> > class Validator(QtGui.QValidator):
> >
> > def validate(self, index_, pos):
> > return QtGui.QValidator.State.Acceptable
>
> This should return (QtGui.QValidator.State.Acceptable, index_, pos)
> instead.
>
> > Running this file generates a QLineEdit, but the moment you edit the
> > contents and have the widget lose focus the following error is emitted:
> >
> > TypeError: invalid result from Validator.validate()
>
> That's correct. This is one of the instances where Qt in C++ expects you
> (or at least allows you) to modify the contents of one of the references
> given as arguments:
>
> https://doc.qt.io/qt-6/qvalidator.html#validate
> "The function can change both input and pos (the cursor position) if
> required."
>
> However, in Python, this isn't possible (both str and int are
> immutable), so PyQt solves this by returning a
> tuple[QValidator.State, str, int] instead. See help() in Python:
>
> >>> help(QValidator.validate)
> Help on built-in function validate:
>
> validate(...) method of PyQt6.sip.wrappertype instance
> validate(self, a0: str, a1: int) -> Tuple[QValidator.State, str,
> int]
>
> or the PyQt docs:
>
> https://www.riverbankcomputing.com/static/Docs/PyQt6/api/qtgui/qvalidator.html
>
> Florian
>
[Attachment #3 (text/html)]
<div dir="ltr">Sure enough, that's working; I should get in the habit of calling \
help, not sure why that never crossed my mind. <div><br></div><div>Sorry for the \
noise,</div><div><br></div><div>Thanks Florian.</div></div><br><div \
class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jun 14, 2023 at \
8:37 AM Florian Bruhin <<a \
href="mailto:me@the-compiler.org">me@the-compiler.org</a>> \
wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px \
0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hey,<br> <br>
> When implementing my own subclass of QValidator, I get a "TypeError:<br>
> invalid result from Validator.validate()" error followed by a segfault \
on<br> > macOS for the following code:<br>
> <br>
> Example:<br>
> <br>
> [...]<br>
> <br>
> class Validator(QtGui.QValidator):<br>
> <br>
> def validate(self, index_, pos):<br>
> return QtGui.QValidator.State.Acceptable<br>
<br>
This should return (QtGui.QValidator.State.Acceptable, index_, pos)<br>
instead.<br>
<br>
> Running this file generates a QLineEdit, but the moment you edit the<br>
> contents and have the widget lose focus the following error is emitted:<br>
> <br>
> TypeError: invalid result from Validator.validate()<br>
<br>
That's correct. This is one of the instances where Qt in C++ expects you<br>
(or at least allows you) to modify the contents of one of the references<br>
given as arguments:<br>
<br>
<a href="https://doc.qt.io/qt-6/qvalidator.html#validate" rel="noreferrer" \
target="_blank">https://doc.qt.io/qt-6/qvalidator.html#validate</a><br> "The \
function can change both input and pos (the cursor position) if<br> \
required."<br> <br>
However, in Python, this isn't possible (both str and int are<br>
immutable), so PyQt solves this by returning a<br>
tuple[QValidator.State, str, int] instead. See help() in Python:<br>
<br>
>>> help(QValidator.validate)<br>
Help on built-in function validate:<br>
<br>
validate(...) method of PyQt6.sip.wrappertype instance<br>
validate(self, a0: str, a1: int) -> Tuple[QValidator.State, str, \
int]<br> <br>
or the PyQt docs:<br>
<a href="https://www.riverbankcomputing.com/static/Docs/PyQt6/api/qtgui/qvalidator.html" \
rel="noreferrer" target="_blank">https://www.riverbankcomputing.com/static/Docs/PyQt6/api/qtgui/qvalidator.html</a><br>
<br>
Florian<br>
</blockquote></div>
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic