Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. Using QDataWidgetMapper() with a tree-based model

Using QDataWidgetMapper() with a tree-based model

Scheduled Pinned Locked Moved Language Bindings
2 Posts 2 Posters 2.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tristam
    wrote on last edited by
    #1

    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 sys

    class 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_()@

    _ 1 Reply Last reply
    0
    • T tristam

      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 sys

      class 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_()@

      _ Offline
      _ Offline
      _qt.py_
      wrote on last edited by
      #2

      @tristam I know this is 10 years later, but have you figured out this issue?

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved