Uploaded image for project: 'Qt for Python'
  1. Qt for Python
  2. PYSIDE-2383

App crashes when view is clicked after reseting a QAbstractItemModel

    XMLWordPrintable

Details

    • Bug
    • Resolution: Out of scope
    • Not Evaluated
    • None
    • 6.5.1.1
    • PySide
    • None
    • Apple M1 Max chip
      macOS Ventura 13.2.1
    • macOS

    Description

      I was under the impression that between calls to beginResetModel() and endResetModel() for a subclass of QAbstractItemModel I could change the model data in whatever way I wanted. However, the behavior described below suggests that either I don't understand how to actually reset a QAbstractItemModel, or there is a bug in PySide6 and PyQt6. Probably the former, so hopefully someone will set me straight.

      1. I began with the editable tree model example from https://doc.qt.io/qtforpython-6/examples/example_widgets_itemviews_editabletreemodel.html. All of those files are here with a few edits as described below.
      2. I added the function reset_from_data() to TreeModel in treemodel.py. This function calls beginResetModel(), then executes the same model initialization code as in the original {}init function, then calls endResetModel().
      3. I added a menu action in mainwindow.py for resetting the model and a slot reset_model() which calls the model's reset_from_data() function.
      4. To test this function, run main.py to bring up the tree model view, then select Reset Model from the Actions menu. If you want you can add some new rows first (use the Actions menu) so you can be sure that the model was indeed reset.
      5. After the model is reset, clicking on the view often causes the app to crash!

       

      Steps to reproduce behavior:

      1. conda create -n test python
      2. conda activate test
      3. pip install PySide6 <-- Note that I found this same behavior occurs with PyQt6 as well.
      4. Downoad the GitHub repo at https://github.com/marcel-goldschen-ohm/PyQt-QAbstractItemModel-ResetModel-Bug/ which has all of the files with my edits (same as the files attached here).
      5. Go to the downloaded repo folder and run python main.py
      6. This will launch an editable tree view widget which should work as expected.
      7. Next select Reset Model from the Actions menu which will call the model's reset_from_data() function that I added (see above).
      8. This visually resets the model as expected!
      9. However, now clicking on the tree view often causes the app to crash, which otherwise does NOT happen if the model was not reset.

       

      Here is an example of the error log when the app crashes:

       {{2023-07-05 17:38:50.335 python[13570:224418] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndexedSubscript:]: index 10 beyond bounds for empty array'

          • First throw call stack:
            (
            0 CoreFoundation 0x000000019ded43e8 __exceptionPreprocess + 176
            1 libobjc.A.dylib 0x000000019da1eea8 objc_exception_throw + 60
            2 CoreFoundation 0x000000019dfb9c94 -[__NSCFString characterAtIndex:].cold.1 + 0
            3 CoreFoundation 0x000000019de45f98 -[__NSArrayM objectAtIndexedSubscript:] + 188
            4 libqcocoa.dylib 0x000000011e174124 _ZN20QCocoaSystemTrayIcon13emitActivatedEv + 269336
            5 libqcocoa.dylib 0x000000011e1741a8 _ZN20QCocoaSystemTrayIcon13emitActivatedEv + 269468
            6 libqcocoa.dylib 0x000000011e17277c _ZN20QCocoaSystemTrayIcon13emitActivatedEv + 262768
            7 QtWidgets 0x000000011c5810b0 ZN9QTreeView14currentChangedERK11QModelIndexS2 + 452
            8 QtCore 0x000000011e299788 _ZN11QMetaObject8activateEP7QObjectPKS_iPPv + 3780
            9 QtCore 0x000000011e45e18c _ZN19QItemSelectionModel15setCurrentIndexERK11QModelIndex6QFlagsINS_13SelectionFlagEE + 324
            10 QtWidgets 0x000000011c50a31c _ZN17QAbstractItemView15mousePressEventEP11QMouseEvent + 880
            11 QtWidgets 0x000000011c579f7c _ZN9QTreeView15mousePressEventEP11QMouseEvent + 260
            12 QtWidgets 0x000000011c2de330 _ZN7QWidget5eventEP6QEvent + 132
            13 QtWidgets 0x000000011c362060 _ZN6QFrame5eventEP6QEvent + 56
            14 QtWidgets 0x000000011c509f3c _ZN17QAbstractItemView13viewportEventEP6QEvent + 988
            15 QtWidgets 0x000000011c57737c _ZN9QTreeView13viewportEventEP6QEvent + 328
            16 QtCore 0x000000011e25025c _ZN23QCoreApplicationPrivate29sendThroughObjectEventFiltersEP7QObjectP6QEvent + 244
            17 QtWidgets 0x000000011c294fcc _ZN19QApplicationPrivate13notify_helperEP7QObjectP6QEvent + 240
            18 QtWidgets 0x000000011c29703c _ZN12QApplication6notifyEP7QObjectP6QEvent + 5100
            19 QtCore 0x000000011e24ff88 _ZN16QCoreApplication15notifyInternal2EP7QObjectP6QEvent + 292
            20 QtWidgets 0x000000011c29558c _ZN19QApplicationPrivate14sendMouseEventEP7QWidgetP11QMouseEventS1_S1_PS1_R8QPointerIS0_Ebb + 808
            21 QtWidgets 0x000000011c2f29b0 _ZNK21QWidgetRepaintManager3rhiEv + 9912
            22 QtWidgets 0x000000011c2f1c68 _ZNK21QWidgetRepaintManager3rhiEv + 6512
            23 QtWidgets 0x000000011c294fec _ZN19QApplicationPrivate13notify_helperEP7QObjectP6QEvent + 272
            24 QtWidgets 0x000000011c295e50 _ZN12QApplication6notifyEP7QObjectP6QEvent + 512
            25 QtCore 0x000000011e24ff88 _ZN16QCoreApplication15notifyInternal2EP7QObjectP6QEvent + 292
            26 QtGui 0x000000011b746794 _ZN22QGuiApplicationPrivate17processMouseEventEPN29QWindowSystemInterfacePrivate10MouseEventE + 1748
            27 QtGui 0x000000011b79daa4 _ZN22QWindowSystemInterface22sendWindowSystemEventsE6QFlagsIN10QEventLoop17ProcessEventsFlagEE + 396
            28 libqcocoa.dylib 0x000000011e117d40 qt_plugin_instance + 54740
            29 CoreFoundation 0x000000019de59a08 {}CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION{} + 28
            30 CoreFoundation 0x000000019de5999c __CFRunLoopDoSource0 + 176
            31 CoreFoundation 0x000000019de5970c __CFRunLoopDoSources0 + 244
            32 CoreFoundation 0x000000019de58310 __CFRunLoopRun + 836
            33 CoreFoundation 0x000000019de57878 CFRunLoopRunSpecific + 612
            34 HIToolbox 0x00000001a7537fa0 RunCurrentEventLoopInMode + 292
            35 HIToolbox 0x00000001a7537c30 ReceiveNextEventCommon + 236
            36 HIToolbox 0x00000001a7537b2c _BlockUntilNextEventMatchingListInModeWithFilter + 72
            37 AppKit 0x00000001a10dd84c _DPSNextEvent + 632
            38 AppKit 0x00000001a10dc9dc -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 728
            39 AppKit 0x00000001a10d0e0c -[NSApplication run] + 464
            40 libqcocoa.dylib 0x000000011e116644 qt_plugin_instance + 48856
            41 QtCore 0x000000011e2598fc _ZN10QEventLoop4execE6QFlagsINS_17ProcessEventsFlagEE + 532
            42 QtCore 0x000000011e250614 _ZN16QCoreApplication4execEv + 112
            43 QtWidgets.abi3.so 0x000000011d1771a4 _ZL25Sbk_QApplicationFunc_execP7_object + 52
            44 python3.11 0x0000000102873e48 cfunction_vectorcall_NOARGS + 296
            45 python3.11 0x000000010281cb04 PyObject_Vectorcall + 76
            46 python3.11 0x000000010291ca58 _PyEval_EvalFrameDefault + 47160
            47 python3.11 0x000000010291029c PyEval_EvalCode + 220
            48 python3.11 0x000000010297601c run_mod + 144
            49 python3.11 0x0000000102975a7c _PyRun_SimpleFileObject + 1264
            50 python3.11 0x0000000102974b38 _PyRun_AnyFileObject + 240
            51 python3.11 0x000000010299b304 Py_RunMain + 3128
            52 python3.11 0x000000010299c198 pymain_main + 1312
            53 python3.11 0x00000001027c0074 main + 56
            54 dyld 0x000000019da4fe50 start + 2544
            )
            libc++abi: terminating with uncaught exception of type NSException}}
             

      Either this is a bug in PySide6 and PyQt6, or I don't understand how to use beginResetModel() ... endResetModel().

      Any ideas?

      OS specific?

      Note that I am on a Mac with an M1 Max processor. This may be relevant?

      Attachments

        1. default.txt
          2 kB
        2. main.py
          0.3 kB
        3. mainwindow.py
          7 kB
        4. pyside2383.zip
          6 kB
        5. treeitem.py
          3 kB
        6. treemodel.py
          7 kB
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            crmaurei Cristian Maureira-Fredes
            marcel-goldschen-ohm Marcel Goldschen-Ohm
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes