[PyQt] modeltest.py for PyQt5?

Nenad Ognjanovic bgr.gyk at gmail.com
Thu Aug 14 15:28:41 BST 2014


Hey Florian,
thanks for the hints, I've applied them and managed to get some progress.

Now the bump is at the following line:
https://github.com/bgr/PyQt5_modeltest/blob/84290469/modeltest.py#L115
seems like the reason is sip.cast again -- hasChildren returns False for
self.model (cast version), and True for self._model (original).

One thing to point out here is that we should take into consideration the way
the models will be used by Qt itself, and replicate that in modeltest.
Here's a hypothetical scenario: when fetching the model from QML, PyQt does
an implicit sip.cast before turning the model over to QML engine (I'm just
making an example, I have no idea what happens here) - we shouldn't work
around the cast in modeltest then. Same goes for QVariant.isValid from
yesterday, modeltest now passes those assertions, but are the assertions
valid from the Qt/QML engine point of view?

On Wed, Aug 13, 2014 at 4:40 PM, Florian Bruhin <me at the-compiler.org> wrote:
> * Nenad Ognjanovic <bgr.gyk at gmail.com> [2014-08-13 16:04:46 +0200]:
>> I've noticed that modeltest.py is no longer present in PyQt source.
>> Since I've started porting the old one to PyQt5, I'm interested in knowing
>> if there was any reason for excluding it in the first place, other than not
>> being compatible anymore?
>> Also, a good question at this point would be: does a PyQt5 version of
>> modeltest.py already exist?
>>
>> The most recent one I've managed to find is the PyQt4 version from tortoisehg
>> repository. I've started porting this version to PyQt5, but I've quickly hit
>> a bump. I'd like to know if anyone here is interested in helping to get this
>> ported, since I'm not familiar with the internals of Qt/PyQt/sip.
>
> Oh, awesome! I wasn't aware of that, and started porting it from C++
> to Python myself, but didn't get very far.
>
>> The source can be found at: https://github.com/bgr/PyQt5_modeltest
>> Not much was changed so far.
>>
>> The first problem I've encountered happens at this point:
>> https://github.com/bgr/PyQt5_modeltest/blob/ea9a34a0/modeltest.py#L238-L239
>> with the following error:
>>
>>     AttributeError: 'NoneType' object has no attribute 'isValid'
>>
>> I've identified that this happens due to sip.cast at line #44, which causes
>> the returned QVariant to be converted into None. When the line #238 is
>> changed to access the original self._model directly, the QVariant is returned
>> correctly.
>
> PyQt5 uses native Python types instead of QVariant - see
> http://pyqt.sourceforge.net/Docs/PyQt5/pyqt_qvariant.html
>
> You can probably either do sip.enableautoconversion(QVariant, False)
> or handle the native types instead.
>
> AFAIK, it returns None when a function returns an invalid QVariant,
> so you probably simply want to change "not qvar.isValid()" to
> "qvar is None", and change all "variant.isValid()" to
> "variant is not None". Same also in line 426, but keep .isValid
> everywhere when a QModelIndex is checked.
>
> Not sure why you'd get an actual QVariant without the sip.cast though.
>
> I'd be happy to help where I can if you run into other issues -
> Mail/XMPP is in the signature, or "The-Compiler" on Freenode.
>
> Florian
>
> --
> http://www.the-compiler.org | me at the-compiler.org (Mail/XMPP)
>              GPG 0xFD55A072 | http://the-compiler.org/pubkey.asc
>          I love long mails! | http://email.is-not-s.ms/
>
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt


More information about the PyQt mailing list