[Interest] QGaphicsItem deletion

Elvis Stansvik elvstone at gmail.com
Thu Jun 16 08:39:27 CEST 2016


Den 15 juni 2016 6:00 em skrev "Igor Mironchik" <igor.mironchik at gmail.com>:
>
> Hello.
>
>
> On 15.06.2016 16:59, Konstantin Shegunov wrote:
>>
>> Hello,
>>
>>
>> On Wed, Jun 15, 2016 at 4:15 PM, Igor Mironchik <igor.mironchik at gmail.com>
wrote:
>>>
>>> Parent of the handles is the same as Line's. But Line own objects of
handles with QScopedPointer.
>>
>>
>> This doesn't sound right. If the handles are not children to the line,
then the line doesn't own them. Leaving semantics aside, it sounds you're
trying to "hijack" the ownership from the parent by using these scoped
pointers.
>
>
> Exactly. I need to Line be as parent of handles but they should be
parented to the scene (read form in my app) because I need boundingRect()
to return exactly bounding rectangle of the line without handles. An I need
handles to be removed with the Line if Line deleted.
>
> I know that I can implement my own method in Line like
boundingRectOfLine() and make handles children of Line. May be it would be
a better solution, but at this time I have such code and it work...

I'm pretty sure this is the right solution. Don't fight the system and make
the handles children of the line, and introduce a special function to get
the line rect. At least that's what I'd do.

You say your current code works, but surely you have some problems with it
or you wouldn't ask? :)

Elvis

>
>
>>
>>>
>>> So my question is it possible that handle will be deleted before Line,
so QScopedPointer will try to free already deleted pointer?
>>
>>
>> From your description I'd guess this is exactly what's happening.
>
>
> Here you aren't right. In d_tor() of QGraphicsScene I see:
>
> while (!d->topLevelItems.isEmpty())
>         delete d->topLevelItems.first();
>
> where topLevelItems are in creating order...
>
> and in QGraphicsItem d_tor()
>
> while (!d_ptr->children.isEmpty())
>             delete d_ptr->children.first();
>
> So when Line deleted it places before handles in children list, and
before Line deleted handles already deleted and removed from children list.
>
>
>>
>> Kind regards.
>>
>>
>> On Wed, Jun 15, 2016 at 4:15 PM, Igor Mironchik <igor.mironchik at gmail.com>
wrote:
>>>
>>> Hi folks,
>>>
>>> Let's say I have QGraphicsItem, let it be Line. This Line has move
handles - another QGraphicsItems, rectangles. Parent of the handles is the
same as Line's. But Line own objects of handles with QScopedPointer.
>>>
>>> When Line creates in c_tor() it creates handles, so handles adds to the
scene after Line. When I delete Line from the scene by hand everything is
fine: Line deletes and QScopedPointers free handles.
>>>
>>> But if I close application and scene destroys with d_tor() in which it
deletes all child items. So my question is it possible that handle will be
deleted before Line, so QScopedPointer will try to free already deleted
pointer?
>>>
>>> Thank you.
>>> _______________________________________________
>>> Interest mailing list
>>> Interest at qt-project.org
>>> http://lists.qt-project.org/mailman/listinfo/interest
>>
>>
>
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20160616/e88f19b7/attachment.html>


More information about the Interest mailing list