Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QTreeWidget - read/display XML?
Forum Updated to NodeBB v4.3 + New Features

QTreeWidget - read/display XML?

Scheduled Pinned Locked Moved Unsolved Qt for Python
6 Posts 2 Posters 613 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.
  • A Offline
    A Offline
    Al3x
    wrote on last edited by Al3x
    #1

    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!!

    1 Reply Last reply
    0
    • Axel SpoerlA Offline
      Axel SpoerlA Offline
      Axel Spoerl
      Moderators
      wrote on last edited by
      #2

      There is no such thing as a QTree, to begin with.
      The Python code posted displays data, 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.

      Software Engineer
      The Qt Company, Oslo

      A 1 Reply Last reply
      0
      • Axel SpoerlA Axel Spoerl

        There is no such thing as a QTree, to begin with.
        The Python code posted displays data, 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.

        A Offline
        A Offline
        Al3x
        wrote on last edited by Al3x
        #3

        @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

        Axel SpoerlA 1 Reply Last reply
        0
        • A Al3x

          @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

          Axel SpoerlA Offline
          Axel SpoerlA Offline
          Axel Spoerl
          Moderators
          wrote on last edited by
          #4

          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.

          Software Engineer
          The Qt Company, Oslo

          A 1 Reply Last reply
          0
          • Axel SpoerlA Axel Spoerl

            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.

            A Offline
            A Offline
            Al3x
            wrote on last edited by Al3x
            #5

            @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!

            Axel SpoerlA 1 Reply Last reply
            0
            • A Al3x

              @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!

              Axel SpoerlA Offline
              Axel SpoerlA Offline
              Axel Spoerl
              Moderators
              wrote on last edited by Axel Spoerl
              #6

              @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.

              Software Engineer
              The Qt Company, Oslo

              1 Reply Last reply
              0
              • Axel SpoerlA Axel Spoerl locked this topic on

              • Login

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