Skip to content
Advertisement

Inserting rows in QTreeView that uses QSortFilterProxyModel

I have a very basic app that displays a tree view and a button that adds items to that tree view, using current selection as a parent. Inserting a first level child works well while inserting the 3d level child fails for some reason (it just is not displayed after inserting is done. I’ve prepared fully verifiable code that you can test yourself, the test case is the following:

  1. Click on any item
  2. Click on the “Add row” button
  3. Click on the newly created item
  4. Click “Add row” button again Expected result: a child item is added Actual result: nothing happens.

Here is the code

main.py

JavaScript

application.py

JavaScript

TreeItem.py

JavaScript

TreeModel.py

JavaScript

tree.py (the view file)

JavaScript

Advertisement

Answer

The problem only occurs when the element editor is activated, which means that in some way the view is not notified of the changes in the model. At that time, dataChanged of the model is not sent to the proxy. An alternative solution is to call the proxy dataChanged and change the state of the expansion or contraction to refresh the view, at the end restore the state of expansion and contraction.

JavaScript

In conclusion if a child is inserted but the view is not updated correctly, this can be verified using another QTreeView and setting self.model as a model.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement