PyQt6: QTableWidget, QTreeWidget, and QlistWidget cannot use custom delegates

James Evans jrevans1 at earthlink.net
Wed Jan 5 19:21:58 GMT 2022


QTableWidget, QTreeWidget, and QListWidget use custom ItemModel classes that,
when coupled with a custom delegate, cannot be properly modified.

When QTreeWidget calls the delegate for a specific item, it passes a
'QAbstractItemModel*' pointer to the delegate 'setModelData' method.  Inside the
'setModelData' method, the model data is typically updated with a call to
'model.setData( . )' which will eventually trigger an 'itemChanged' signal to be
emitted by the QTreeWidget.

The problem arises because the ItemModel classes used by these widgets are not
part of the public Qt API.  If the QTreeWidget model QTreeModel was part of the
public API, then sip would have actually passed in a pointer to the derived
QTreeModel class, and everything would work fine.  Instead calling
'model.setData' results in a C++ call like so: "::QAbstractItemModel::setData( .
)", which will always fail (as designed).

The funny thing is, that this actually used to work (for many years) before I
recently updated the version of the tools I am using.
Qt/PyQt: 5.15.1 --> 6.2.2
GCC: 10.2.0 --> 11.2.0

I have attached a script that I can run successfully in my PyQt5 environment,
but fails under the PyQt6 environment.
NOTE: I wrote this script to highlight the problem, not as an example of good
code, so no judgement please. :)

--James
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test_delegate.py
Type: application/octet-stream
Size: 4881 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20220105/ffe9a435/attachment.obj>


More information about the PyQt mailing list