Results 1 to 1 of 1

Thread: Display properties of single item in QTableView based on selection in QTreeView

  1. #1
    Join Date
    Jul 2015
    Posts
    1
    Qt products
    Platforms
    Unix/X11 Windows

    Default Display properties of single item in QTableView based on selection in QTreeView

    Hi,

    This is my first time using PyQt. I am attempting to parse an XML file and display the hierarchy in a QTreeview and allow the user to edit the properties of a particular element in a QTableView. I have successfully parsed the XML using etree and populated a QStandardItemModel. I've been trying to figure out the QItemViewDelegate but have not had much success yet.

    My issue is this: When I select an item in the QTreeView how can I update the QTableView to only display the properties of the selected element?

    My other issue is I still need to figure out how to populate the attributes in the QStandardItemModel (I assume I need to add to qt.UserRole with setData, but I have not gotten that far in yet).

    As an example: I want to select Tab1 in the QTreeView and in the QTableView I want to see only

    Field | Value
    -------------
    Name | tab1
    img | xyz.jpg
    uid | 123

    Below is my python code
    Qt Code:
    1. import sys
    2. from PyQt4.QtCore import *
    3. from PyQt4.QtGui import *
    4. from PyQt4.uic import *
    5. import xml.etree.ElementTree as ET
    6.  
    7. class Window(QMainWindow):
    8.  
    9. def __init__(self):
    10. QMainWindow.__init__(self)
    11.  
    12. # -- Load QT .ui file --
    13. self.ui = loadUi('test.ui')
    14. # ----
    15.  
    16. # -- Display UI using QT ui layout --
    17. self.ui.show()
    18. # ----
    19.  
    20. # -- Initialize data models for Treeview and Tableview
    21. self.modelTreeView = QStandardItemModel()
    22. self.modelTableView = QStandardItemModel()
    23. self.ui.XMLTreeView.setModel(self.modelTreeView)
    24. self.ui.ItemProperties.setModel(self.modelTreeView)
    25. # ----
    26.  
    27. #UI Tree view item selection
    28. self.connect(self.ui.XMLTreeView.selectionModel(), SIGNAL("selectionChanged(QItemSelection, QItemSelection)"), fnXMLUpdate)
    29. tree = ET.parse('test.xml')
    30. root = tree.getroot()
    31. temp = tree.findall(".//tab[@uid='123']")
    32. parseXML(self.modelTreeView, root)
    33. # ----
    34.  
    35. #UI Decorations
    36. self.modelTreeView.setHorizontalHeaderLabels([self.tr("Object")])
    37. self.modelTableView.setHorizontalHeaderLabels(["Property","Value"])
    38. # ----
    39.  
    40.  
    41. def fnXMLUpdate(current,previous):
    42. print("new item selected in tree view")
    43. index = window.ui.XMLTreeView.selectedIndexes()[0]
    44. print(index)
    45. crawler = index.model().itemFromIndex(index)
    46. print "Node Name: %s" % crawler.data(Qt.DisplayRole).toString()
    47. uid=crawler.data(Qt.UserRole).toString()
    48. print "Node uid: %s" % uid
    49.  
    50. print("end new tree view")
    51.  
    52. item = QStandardItem()
    53. item.setData(crawler.data(Qt.UserRole), Qt.DisplayRole)
    54. item.setData(crawler.data(Qt.UserRole), Qt.UserRole)
    55.  
    56. window.ui.ItemProperties.setRootIndex(index)
    57. #window.modelTableView.setRootIndex(index)
    58. #window.modelTableView.appendRow(item)
    59. #window.modelTableView.setHorizontalHeaderLabels(["Property","Value"])
    60.  
    61. def parseXML(parent, node):
    62. for element in node:
    63. item = QStandardItem()
    64. item.setData(element.attrib.get('name'), Qt.DisplayRole)
    65. item.setData(element.attrib.get('uid'), Qt.UserRole)
    66. parseXML(item, element)
    67. parent.appendRow(item)
    68.  
    69. if __name__ == "__main__":
    70.  
    71. app = QApplication(sys.argv)
    72. window = Window()
    73. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

    Attached is the XML and UI file
    Attached Files Attached Files

Similar Threads

  1. Replies: 1
    Last Post: 31st October 2014, 20:45
  2. Have a single selection of item between 2 QListWidgets
    By salik89 in forum Qt Programming
    Replies: 5
    Last Post: 29th January 2014, 18:53
  3. Display QStandardItem Properties in a QTableView row
    By Phlucious in forum Qt Programming
    Replies: 3
    Last Post: 18th December 2012, 01:19
  4. Replies: 0
    Last Post: 18th April 2011, 02:21
  5. Replies: 14
    Last Post: 9th November 2006, 08:35

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.