I have a QAbstractItemModel with children on both the first and second column, like this:
item item
├ item subtree
| ├ sub
| ├ sub
| | ├ subsub
| | └ subsub
| └ sub
├ item2 item
├ item
└ item
├ item item
| ├ sub
| └ sub
└ item item
With QTreeView, I want to display subtree, eg. setting subtree as the rootIndex. When using QTableView, I have the subtree correctly displayed, however, only for the first level, as QTableView ignores nested indexes. With QTreeView no items are displayed.
I know that QTreeView expects the children to be on the first column, but I would assume this to be relative to the rootIndex. To add, setting the rootIndex to item2, I get the intended behavior, although for the wrong column / subtree.
One solution would be to separate the model into two smaller models, but I want to understand what the problem is.
I am thankful for any comments on how to display the subtree with a QTreeView.
It seems the unexpected behavior was due to some errors in the model implementation. QTreeView now sees the subtree correctly.
Related
Versions
pyqt5
python 3.6
Setup
I'm having a few issues with the QTreeView class.
The QTreeView is configured as:
treeView.setSelectionBehavior(QAbstractItemView.SelectRows)
treeView.setSelectionMode(QAbstractItemView.SingleSelection)
There are no custom style sheets on this QTreeView
I've implemented a custom model for the view, and the flags() definition is:
def flags(self, index):
if not index.isValid():
return 0
f = super(RecordTreeModel, self).flags(index)
f = f | Qt.ItemIsSelectable | Qt.ItemIsEnabled
return f
Is there anything else relevant to the situation? I'm not doing anything fancy with my model. No custom painting, no special selection requirements -- everything is straight out of basic model examples besides the way the data structure is accessed.
Issues
Row Selection
When clicking on any column besides the first column, the row will not be selected.
Column Hovering
When hovering over any column besides the first column, the row will not be highlighted
Child Hovering
No child rows are ever highlighted when the mouse hovers over them.
I've been banging my head (and google's search bar) against these problems for several hours, to no avail.
Please let me know if I can provide any additional information!
I have a QTableWidget and want items to be selectable depending on what's already selected (only if they have the same content in second column).
I know how to make items selectable or not. But everything should remain selectable so the user can select a single item to start the selection fresh. Only adding to a selection by keeping shift or ctrl pressed should allow/select only suitable items.
Where can I hook into to make adding to a selection only accept suitable rows?
Note: I'm using PySide but I can work from C++ or PyQt code just fine.
You could connect to the QTableWidget.itemSelectionChanged signal and modify the Qt.ItemIsSelectable flags of all the other items in the table based off the selection.
from itertools import product
table = QtGui.QTableWidget()
table.itemSelectionChanged.connect(self.on_itemSelectionChanged)
#QtCore.pyqtSlot()
def on_itemSelectionChanged(self):
sel_items = self.table.selectedItems()
# get all items in table
items = []
for r, c in product(range(self.table.rowCount()), range(self.table.columnCount())):
items.append(self.table.item(r, c)
# Loop through all the items in the table and
# set the selection flag based of already selected items.
for item in items:
if can_be_selected:
item.setFlags(item.flags() | QtCore.Qt.ItemIsSelectable)
else:
item.setFlags(item.flags() & ~QtCore.Qt.ItemIsSelectable)
how to easily make duo QAbstractListModel × QComboBox (not reimplementing QComboBox)
so that item representation on selection differs from one on DropDownList
Background:
I have model (inherited from QAbstractListModel) which flattens other model derived QAbstractItemModel, which however represents tree structure
Items are in the same order as they are in the tree model
I wanted to indent them, for better orientation in selection,
however once selected, it looks strange, when the sole selected item is indented
i.e. having this structure (verbatim how I assume it being seen in QComboBox DropDown list
parent 1
-> child 1.1
---> subChild 1.1.1
-> child 1.2
-> child 1.3
---> subChild 1.3.1
---> subChild 1.3.2
parent2
and once selected, I want it to show only
subChild 1.3.2
now it shows
---> subChild 1.3.2
as far as I know, both representations go through model::data(index, role=Qt::DisplayRole), so I can't differentiate them there
PS: In case there is some QComboBox-like directly working with models containing trees, even better
I am having a difficult time overloading QTreeWidgetItem's < operator.
I have a list of QTreeWidgetItems, some top-level items and some children. When the user activates the "date created" column to sort the tree, the sort occurs at the child level.
In other words, imagine a tree like this:
Item A - latest modification 9/24/12
3rd Modified - 9/21/12
2nd Modified - 9/15/12
1st Modified - 9/10/12
Item B - latest modification 9/23/12
2nd Modified - 9/23/12
1st Modified - 9/22/12
When the date column is activated to sort ascending, I would expect the return result to be: Item A, then Item B... but instead, the sort occurs on the 1st modified date of the item. I am returned with Item B then Item A. Even though none of the top-level items are expanded, or active.
If you use a QWidgetTree to dispay your QTreeWidgetItems list :
You can add your 2 firsts level item (item A and B), sort your QwidgetTree and then add your low level items.
so your tree will be sorted by latest modification
I need a hierarchical grid that shows data in columns for the parent rows as well as the child rows.
- a parent | 1234 | data | data |
a child | 2222 | data | data |
a child | 212 | data | data |
I've tried to make ADG work, but by default it has all columns except the grouping column blank for parent rows. I think I could use item renderers to push the data out there.
My blocking problem is that my grouping does not seem to work at all. I can see the data when I set dataProvider=MyArrayCollection, but
<mx:GroupingCollection id="GroupingCollection"
source="{this.specificReportData.gridData}">
<mx:Grouping label="childName">
<mx:GroupingField name="parentName" />
</mx:Grouping>
</mx:GroupingCollection>
does not work: no data appears. I've tried having an explicit "childname" column, and not having one. I've tried wrapping it in a HierarchicalCollectionView, but that doesn't work either. I've walked through the basic grouping tutorials and my code and data look like it follows the pattern.
Any ideas?
You must not include parents in your datasource. You need a datasource like this:
a child | 2222 | data | data | parentid | parentname
a child | 212 | data | data | parentid | parentname
And set the groupingfield to parentid or parentname. The grouping makes a hierarchy out of a flat datasource and thus creates the parents for you.
The answer is that the AdvancedDataGrid is not built to do what I want.
It is great at grouping data with common values; it is not good at grouping data where the actual parent elements exist as rows themselves. I also require the sorting and filtering apply to the parent elements only (based on their own data); any visible parent can show their children, and the children never sort.
My two-stage solution is first to process the data by iterating through it and creating an array of parent elements and adding a children array property to them that holds the child elements. Note that these are arrays of references; I am not copying the actual data.
Next, I apply any sorting and filtering to the parent array (perhaps by using an ArrayCollection wrapper) and iterate through the parents, copying the visible ones (and their children if the parent is marked expanded) to a display array. My grid them uses the display array as a simple (e.g. not grouped) dataProvider. A custom item renderer will indent the children and change the expanded property of the parent elements, which will regenerate the display array.
This is not only a simpler and more intuitive solution, it does not require a reader or maintainer to understand the idiosyncrasies of ADG (my motto: No Clever Code).
Thank you Thomas for trying. I'm giving your reply a +1 even though I'm not marking it as the answer.
Cheers