beeware / beeware/toga

Qt Tree Insert/Remove Rows Issues

Open
#4,202 2 comments 0 reactions 0 assignees View on GitHub
bug qt
Dominant language
Python
Stars
5.4k
Forks
827
Avg merge
9h 45m
Merged PRs (30d)
58

Description

### Describe the bug

When the structure of the Qt Tree widget changes, we have to reset the selection and expanded nodes to prevent dead references to objects causing segfaults. See #4112 for discussion. In summary:

- to preserve the selection and expanded nodes, Qt needs to be told when things are about to change so that it can make the changes to the indexes of the selection and expended nodes stored on the tree view and related objects based on the anticipated changes.
- doing this requires consistent state between the Toga data source and the Qt view and model
- with just a single call after the Toga source has changed, we end up in a situation where toga indexes are in the "after" state, but the indexes of selections etc. are in the "before" state, when leads to bad indexes with references to dead objects

### Describe the solution you'd like

This can be fixed by two things:
- changing `QModelIndex` to hold a ref to the parent, not the node
- getting tree sources to emit pre-insert and pre-delete events

The latter is a major change and was beyond the scope of #4112.

### Describe alternatives you've considered

Using a `QTreeWidget` instead of a `QTreeView` would also solve these problems, but would impose performance limits on the widget at large data set sizes.

There may be approaches where the model tracks the tree structure independently (as we currently do for columns) and uses that for getting indices, but that has performance costs associated with keeping the shadow tree updated for large data sets.

### Additional context

See #4028 for a discussion of pre-change notifications.

There are commits in #4112 that implement most of the required changes.

Contributor guide

Open the contributing guide

Research direction

Read the discussion in #4112 and #4028, then inspect the commits in #4112 related to the Qt tree model and data-source notifications. Trace how insert and delete changes propagate between the Toga source, Qt model, and view; done means selection and expanded nodes remain valid without references to deleted objects or segfaults.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
desktop
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.