Reputation: 6147
How can I expand a QTreeView item knowing it's modelIndex. I'm trying to call the following piece of code and it doesn't throw any errors but it does not expand the treeview item...
parentIndex = self.categoryModel.indexFromItem(parent)
if parentIndex.isValid():
self.uiTreeView.setExpanded(parentIndex, True)
To test, select the first item in the list and click the Add button. When a user adds a new category i want it to expand the item it's being added to.
import os, sys
from Qt import QtWidgets, QtGui, QtCore
################################################################################
# Widgets
################################################################################
class CategoryView(QtWidgets.QWidget):
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.resize(250,400)
self.uiAdd = QtWidgets.QPushButton('Add')
self.categoryModel = QtGui.QStandardItemModel()
self.categoryModel.setHorizontalHeaderLabels(['Items'])
self.categoryProxyModel = QtCore.QSortFilterProxyModel()
self.categoryProxyModel.setSourceModel(self.categoryModel)
self.categoryProxyModel.setFilterCaseSensitivity(QtCore.Qt.CaseInsensitive)
self.categoryProxyModel.setSortCaseSensitivity(QtCore.Qt.CaseInsensitive)
self.categoryProxyModel.setDynamicSortFilter(True)
self.uiTreeView = QtWidgets.QTreeView()
self.uiTreeView.setModel(self.categoryProxyModel)
self.uiTreeView.sortByColumn(0, QtCore.Qt.AscendingOrder)
self.uiTreeView.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
self.layout = QtWidgets.QVBoxLayout()
self.layout.setContentsMargins(0,0,0,0)
self.layout.setSpacing(0)
self.layout.addWidget(self.uiAdd)
self.layout.addWidget(self.uiTreeView)
self.setLayout(self.layout)
# Selections
self.categorySelection = self.uiTreeView.selectionModel()
# Signals
self.uiAdd.clicked.connect(self.slotAddNewCategory)
parent = self.categoryModel.invisibleRootItem()
parent.appendRow(QtGui.QStandardItem('Fruit'))
# Methods
def getSelectedItems(self):
items = []
for proxyIndex in self.categorySelection.selectedIndexes():
sourceIndex = self.categoryProxyModel.mapToSource(proxyIndex)
item = self.categoryModel.itemFromIndex(sourceIndex)
items.append(item)
return items
def slotAddNewCategory(self):
text, ok = QtWidgets.QInputDialog.getText(self, 'Input Dialog', 'Enter your name:')
if ok:
item = QtGui.QStandardItem(text)
parent = self.categoryModel.invisibleRootItem()
items = self.getSelectedItems()
if len(items) == 1:
parent = items[0]
parent.appendRow(item)
parentIndex = self.categoryModel.indexFromItem(parent)
print parentIndex.data()
if parentIndex.isValid():
self.uiTreeView.setExpanded(parentIndex, True)
################################################################################
# Unit Testing
################################################################################
def test_CategoryView():
app = QtWidgets.QApplication(sys.argv)
ex = CategoryView()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
pass
test_CategoryView()
# test_PainterSettingsDialog()
Upvotes: 0
Views: 1040
Reputation: 6147
Answer found here... How to expand top-level QTreeview items
parentIndex = self.categoryModel.indexFromItem(parent)
px = self.categoryProxyModel.mapFromSource(parentIndex)
if parentIndex.isValid():
self.uiTreeView.setExpanded(px, True)
Upvotes: 1