Details
Description
Preface
I have an adaption of the Qt Flow Layout Example (http://doc.qt.io/qt-5/qtwidgets-layouts-flowlayout-example.html) which works fine on its own.
I added more methods for convenient layout management like insertWidget. I took a look at QBoxLayout in the Qt source to have a reference of what the implementation should be. I firmly believe that there is nothing wrong with my code, and the fact that it is working correctly if I use PyQt4 confirms it.
The problem
When I implement the insertWidget method I must create a QWidgetItem to wrap the widget in a QLayoutItem. The Qt source uses the QLayoutPrivate::createWidgetItem method which is private as its name states, so I cannot use that. Not being able to use it should not be a problem normally since it only returns a QWidgetItem and does nothing else.
However, there is an issue with the QWidgetItems that I create on the Python side, that makes the sample code crash. Creating and using the QWidgetItems seem to work but deleting them on the C++ side does not.
By logging the calls to virtual methods I see that QLayout.addChildWidget calls removeWidgetRecursively and that will crash on the line "delete lay->takeAt( i);".
If I do not call the QLayout.addChildWidget method but delete the QWidgetItem on the Python side with shiboken(2).delete it works. So deleting the QWidgetItem, that had been created in Python, on the C++ side has some issue.
Sample code
I attached a smaller version of my code. The buttons are in the flow layout and clicking them moves them to the first place with insertWidget(0, w). You can decide which binding you use by uncommenting/commenting the desired block. Setting the config variable called "DELETE_QWIDGETITEMS_ON_PYTHON_SIDE" to True makes a hacky demonstration that deleting the QWidgetItems on the Python side works.