Using QDataWidgetMapper() with a tree-based model
-
Is it possible to use QDataWidgetMapper() to map widgets to a tree-based model? Since widgets are mapped by integer (column or row, rather than two-dimensional index) based "sections", and the QStandardItemModel resets the row value for a node's child items, without any additional tweaks there will be ruinous overlap between the sections of child and parent widgets. The only tweak I can think of is subclassing QStandardItemModel to entirely change the implementation of index values. Beyond that, I can only think to do updates to the model from widgets manually, but it would be nice if it were possible to utilize the mapper. I assumed that setting (and then resetting) the root index through the mapper class's setRootIndex() method may be the solution to this, but either it's not actually the solution or I'm implementing it incorrectly. Below is a full dialog window demonstrating my dilemma.
@
#!/usr/bin/python
from PySide.QtCore import *
from PySide.QtGui import *
import sysclass Form(QDialog):
def init(self, parent=None):
super(Form, self).init(parent)
self.setWindowTitle("Test Widget Mapper")self.model = QStandardItemModel(self)
self.rootitem = self.model.invisibleRootItem()self.treeview = QTreeView()
self.treeview.setModel(self.model)self.mapper = QDataWidgetMapper(self)
self.mapper.setSubmitPolicy(QDataWidgetMapper.AutoSubmit)
self.mapper.setModel(self.model)
self.mapper.setOrientation(Qt.Vertical)
self.mapper.setRootIndex(self.rootitem.index())create widgets
self.line_edit1 = QLineEdit()
self.line_edit2 = QLineEdit()
self.line_editsub = QLineEdit()create and set layout
layoutw = QHBoxLayout()
layout = QVBoxLayout()
layout.addWidget(QLabel("Item 1:"))
layout.addWidget(self.line_edit1)
layout.addWidget(QLabel("Subitem 1:"))
layout.addWidget(self.line_editsub)
layout.addWidget(QLabel("Item 2:"))
layout.addWidget(self.line_edit2)
layoutw.addWidget(self.treeview)
layoutw.addLayout(layout)
self.setLayout(layoutw)create and map items
self.item1 = QStandardItem("Item 1")
self.item2 = QStandardItem("Item 2")
self.subitem1 = QStandardItem("Sub item 1")
self.rootitem.appendRow(self.item1)
self.item1.appendRow(self.subitem1)
self.rootitem.appendRow(self.item2)
self.mapper.addMapping(self.line_edit1, 0)
self.mapper.addMapping(self.line_edit2, 1)
self.mapper.toFirst()self.treeview.expandAll()
app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()@