QTreeWidget - read/display XML?
-
Hello everyone,
Im trying to create a QTree that displays XML content. Basic xml that has multiple entries for various VPN ips.
The script as is now works but thats with the actual 'Data' hardcoded inside the script. I need to read an xml and display it equivalent to how it does now.
Ive read a few articles but im struggling to get it working..Could someone please help?!
import sys from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QTreeWidgetItem #import xml.etree.ElementTree as ET #tree = ET.parse('data.xml') #root = tree.getroot() class Widget(QtWidgets.QWidget): def __init__(self, parent=None): super(Widget, self).__init__(parent) lay = QtWidgets.QVBoxLayout(self) tree = QtWidgets.QTreeWidget() tree.setColumnCount(3) tree.setHeaderLabels(["VPN", "2", "3"]) lay.addWidget(tree) ######################################################### ## new - not working # f = open("data.xml", 'r').read() # self.printtree(f) # # def printtree(self, s): # tree = ET.fromstring(s) # a=QTreeWidgetItem([tree.tag]) # self.tree.addtTopLevelItems(a) # # def displaytree(a,s): # for child in s: # branch=QTreeWidgetItem([child.tag]) # a.addChild(branch) # displaytree(branch,child) # displaytree(a,tree) ########################################################### ## new - not working ## working data = { "USA": ["18.122.x.xx.ovpn", "0.22.0.0.ovpn", "11.0.0.0.ovpn"], "Europe": ["77.0.0.0.ovpn", "66.0.0.0.ovpn"], "Asia": ["99.0.0.0.ovpn","0.99.0.0.ovpn"]} items = [] for key, values in data.items(): item = QTreeWidgetItem([key]) for value in values: ext = value.split(".")[-1].upper() child = QTreeWidgetItem([value, ext]) item.addChild(child) items.append(item) tree.insertTopLevelItems(0, items) tree.expandAll() tree.itemClicked.connect(self.onItemClicked) @QtCore.pyqtSlot(QtWidgets.QTreeWidgetItem, int) def onItemClicked(self, it, col): print(it.text(col)) ## working ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ##################################################################### if __name__ == '__main__': import sys app = QtWidgets.QApplication(sys.argv) w = Widget() w.show() sys.exit(app.exec_())
Thanks!!
-
There is no such thing as a
QTree
, to begin with.
The Python code posted displaysdata
, which is actually a JSON object, no XML.Displaying XML in a tree view is a much more complex task.
I suggest to start by reading some documentation here. -
@Axel-Spoerl
Yes im aware of that, thats why im asking for help.
What do you suggest i look at in the link you noted?I was thinking perhaps a JSON file would be easier? but when i searched i couldnt find a way to import JSON
-
Please share all information from the beginning.
You didn't say anything about JSON, I had to find it out.couldnt find a way to import JSON
=> import JSON to what? XML looks like an overkill, given the fact that you actually import an array IP addresses with a (redundant?)
ovpn
suffix per continent. -
@Axel-Spoerl
Ive shared all information in my first post. The example functions, but i wish to do it XML. or perhaps JSON since you say its so much more complex.not import, Read JSON.
I thought of XML because its an easy format. because for every vpn.opvn file i need to specify other data, IP, Port, Protocol, etc. Json is more clumsy...
What specifically are you recomending i read in the Doc Link you provided. It lists alot of stuff!
-
@Al3x said in QTreeWidget - read/display XML?:
Ive shared all information in my first post.
Why start a duplicate topic then? How should anybody know that this is a duplicate?
You are wasting the time of voluntary contributors. Duplicate topics are discouraged. Don't do it again.
I'll lock this topic. Continue here. -