Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-103808

Inconsistency between QObject and QWidget destroyed emitted and QPointer

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P3: Somewhat important P3: Somewhat important
    • None
    • 5.0.0, 5.15.2, 6.2.4
    • None
    • Windows x64
    • Windows

      I think there's an inconsistency between QObject and QWidget behaviour with respect to emitting destroyed signal and clearing of QPointers.

       

      If I have a std::list<QPointer<QObject>> tracking some non-QWidget QObjects and want to remove items from the list when the QObject is destroyed I can connect to the destroyed signal and check if the QPointer isNull(), as per documentation https://doc.qt.io/qt-5/qobject.html#destroyed where is says "This signal is emitted immediately before the object obj is destroyed, after any instances of QPointer have been notified".

      However, if I have a std::list<QPointer<QWidget>> doing the same, then the QPointer is not cleared before destroyed is emitted. I think this is related to the change for QTBUG-24672 where destroyed is emitted in ~QWidget().

      I have attached a project to demonstrate the difference in behaviour. The fix is relatively simple - in the lambda check if the object being destroyed is in the list.

      I've only tried on Windows, but can't see why it would be any different on any other platform.

       

        1. qwidget_destroyed.zip
          3 kB
          Michael Partridge
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            qt.team.quick.subscriptions Qt Quick and Widgets Team
            michael.partridge Michael Partridge
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:

                There are no open Gerrit changes