I wasn't able to reproduce your problem from the code you provided. You may
need to consolidate it into a single file that can be executed, or be more
specific about the conditions under which you are executing it. But either
way, I agree with your idea that it is related to calling
QScrollArea.setWidget. It looked to me that the problem was trying to set
the parent on the previous widget before you replace it with another one.
My expectations of what setWidget should do would end up still doing
something with the previous widget even though you changed its parent. You
can actually see exactly what it is doing here:
https://github.com/qt/qtbase/blob/dev/src/widgets/widgets/qscrollarea.cpp#L255
It looks like setWidget will always delete the previous one if it is still
set, because the QScrollArea takes ownership of it when you first set it.
But you can explicitly take the ownership back instead of having the
QScrollArea manage the lifetime:
https://doc.qt.io/qt-5/qscrollarea.html#takeWidget
So maybe this is the fix:

prev_widget = self.scrollArea.takeWidget()


On Fri, Oct 16, 2020 at 5:49 AM vince touache <fruityfr...@gmail.com> wrote:

> it looks like QSCrollArea.setWidget() does a lot more than what it seems.
> And seems to be partially related to my issue. E.g.
>
> *If the scroll area is visible when the widget is added, you must show()
> <https://doc.qt.io/qt-5/qwidget.html#show> it explicitly.*
>
> or
>
> *Note that You must add the layout of widget before you call this
> function; if you add it later, the widget will not be visible - regardless
> of when you *show() <https://doc.qt.io/qt-5/qwidget.html#show>* the
> scroll area. In this case, you can also not *show()
> <https://doc.qt.io/qt-5/qwidget.html#show>* the *widget *later.*
>
> As a temp solution, I use a QStackedWidget() to keep all my widgets and
> display them when needed, but I would really like to understand why my
> widget is destroyed despite the reparenting, in the example above
>
> cheers
> Le jeudi 15 octobre 2020 à 09:13:42 UTC-4, vince touache a écrit :
>
>> hello,
>>
>> I have this good old *internal C++ object already deleted *and can't
>> understand where it comes from
>> To summarize, I have a QMainWindow with a QScrollArea, and I want to
>> parent/unparent widgets into this QScrollArea. Ofc I make sure to re-parent
>> the widget I'm about to remove, to make sure it never gets unparented and
>> garbage collected, but it doesn't change anything....
>>
>> Since I still don't know how to format code on google groups (yay \o/), I
>> updated some code on github <https://github.com/fruityvince/debug> if
>> someone wants to try it , and here is a snippet to reproduce the error
>>
>> import main;import foo
>> m = main.Main() # this is my main window
>> m.show()
>> # now I create 2 widgets, and I want to display the first one in my
>> scrollArea, then the second, then come back on my first
>> bar1 = foo.Bar("bar1")
>> bar2 = foo.Bar("bar2")
>>
>> # now I parent it to my scrollArea
>> m.something_triggered(bar1)
>> m.something_triggered(bar2) # this is where bar1 gets deleted, even
>> though i reparent it before replacing with bar2
>> >>> # RuntimeError: Internal C++ object (PySide2.QtWidgets.QLabel)
>> already deleted. #
>>
>> Can anyone explain me why / how my bar1 gets deleted, even though I
>> parent it to my mainWindow to prevent garbage collection?
>>
>> Thank you
>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to python_inside_maya+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/a6fef101-6378-4b3d-9bac-00c95936afd3n%40googlegroups.com
> <https://groups.google.com/d/msgid/python_inside_maya/a6fef101-6378-4b3d-9bac-00c95936afd3n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to python_inside_maya+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA0i%3DSB0GCsgYwF_0xL7GsQkTv%2B0A7g2iB3r8TQyLZV%2BWg%40mail.gmail.com.

Reply via email to