This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Help Populating a QTreeWidget

Tags: None
(comma "," separated)
User avatar
PuercoPop
Registered Member
Posts
15
Karma
0
OS

Help Populating a QTreeWidget

Mon Sep 14, 2009 8:57 pm
Hi, I want to make a Simple Tree View using QTreeWidget in the format
Code: Select all
+channel1
  -device1
  -device2
  -device3
+channel2
  -device4
  -device5
  -device6

getting the data from a file using the following format( the ip address is an optional parameter):
Code: Select all
channel:ELC_Ayacucho
device:F650_S1
device:F35_S2
device:F35_S3,192.168.1.77
device:T60_1
channel:HDA_Pallasca
device:F650_S1
device:F650_S2,192.168.1.2

I have found out that I can use the QTreeWidget using the following pattern:
Code: Select all
list = QtGui.QTreeWidget()
list.setColumnCount( 1 )
      
<top_var> = QtGui.QTreeWidgetItem([ <channel> ])
<var> = QtGui.QTreeWidgetItem( <top_var>,[ <device> ])
...same for next device until a new channel is encountered
   
list.addTopLevelItem( <top_var> )

Now I need help with two things.

1st, how can I populate the QTreeWidget without using variable names as I don't know before hand how many channels and devices associated with it are there.

2nd, I wanted to first do a list of channels and a list of lists of devices
so I wrote the following code:
Code: Select all
m = re.compile("(.*):(.*)\n")
#El 1er grupo es determina si es canal o device, lo demas sigue
m_ip = re.compile("(.*),(.*)")
#el 2do grupo es el ip en caso hubiera, el primero sigue
      
subestaciones = []
equipos = []
devices = []
channel = None
   
conf = open( 'General.conf','r' )
for line in conf:
   t = m.match(line)
   if t is not None:
      if t.group(1) == "channel":
         if channel is not None:
            #Codigo de Refresco
            subestaciones.append( channel )
            equipos.append( devices )
            del devices[:]
         channel = string.strip(t.group(2))
      elif t.group(1)  == "device":
         t1 = m_ip.match(string.strip( t.group(2) ))
         if t1 is None:
            devices.append( string.strip( t.group(2) ) )
         else:
            devices.append( t1.group(1)  )
   else:
      print "no Match"
subestaciones.append( channel )
equipos.append( devices )

The problem with it is that when I append the list 'devices' to 'equipos' it appends a reference to it and after I modify it it updates it for the 1st instance. So how should I do the list of lists to prevent that from happeing? Or should I use a different way of storing the data?

Thanks for your time, Hope you can help me out.


PuercoPop, proud to be a member of KDE forums since 2008-Oct.
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS

Re: Help Populating a QTreeWidget

Tue Sep 15, 2009 2:05 am
I would recommend looking at QMap and QList for storing your data. I would store it as follows:
QList< Channel >
QMap< Channel, Device >
QList< QTreeWidgetItem >

Then I would iterate over the channel list, and call values( Channel ) on the QMap to get the list of devices in that channel. I would then create the QTreeWidgetItem and append it to a generic list to store them.

For retrieving the list of Devices, just call values() on the QMap.


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]


Bookmarks



Who is online

Registered users: Bing [Bot], daret, Google [Bot], sandyvee, Sogou [Bot]