How to tally and displays items in QListWidget

Post here if you need help with creating a Graphical User Interface in Python.

How to tally and displays items in QListWidget

Postby salik89 » Mon Jan 20, 2014 4:47 pm

Hi all, I am new to Python and I am not sure if this is the right place for me to place my thread too.
Nonetheless I am encountering a problem in regards to the program I am writing.

I am trying to create 2 QListWidgets, mainList and refList. Both List are folder names derived from directories (main from user com and ref from server)
mainList - Tally its items against the refList and displays the items it has in common with the refList. Other miscellaneous items will not be displayed
refList - Displays the correct info (acts more like the reference); no info will be changed here, it's fixed

Thus, when the user executes/run the program, the ListWidget will displays the items on startup, especially for mainList where it tallies against the refList

For examples (All T0X are folder names):
mainList has - T01, T02, T03, test.txt and test.mp3
refList has - T01, T02, T03, T04, T05

So when user runs the program, the List Widget will display
mainList has - T01, T02, T03
refList has - T01, T02, T03, T04, T05

Can someone guide me how to do it?
Last edited by stranac on Mon Jan 20, 2014 5:20 pm, edited 1 time in total.
Reason: First post lock.
salik89
 
Posts: 13
Joined: Mon Jan 20, 2014 4:36 pm

Re: How to tally and displays items in QListWidget

Postby Yoriz » Mon Jan 20, 2014 9:38 pm

Hi welcome to the forum.
Please read the 'new users, read this' link in my signature.

There are various ways and it doesn't really have anything to do with QListWidget, you alter the list before you give it to the QListWidget.
Code: Select all
main_list = ['T01', 'T02', 'T03', 'test.txt', 'test.mp3']
ref_list = ['T01', 'T02', 'T03', 'T04', 'T05']

print main_list

items = []
for item in main_list:
    if item in ref_list:
        items.append(item)

print items

print [item for item in main_list if item in ref_list]

print filter(lambda x: x in ref_list, main_list)

print list(set(main_list).intersection(set(ref_list)))

Output
Code: Select all
['T01', 'T02', 'T03', 'test.txt', 'test.mp3']
['T01', 'T02', 'T03']
['T01', 'T02', 'T03']
['T03', 'T02', 'T01']
['T01', 'T02', 'T03']
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 839
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: How to tally and displays items in QListWidget

Postby salik89 » Tue Jan 21, 2014 6:30 am

Hi Yoriz, thank you for getting back to me.
Currently I am unable to test it out as the mainList is in my school server.
As I have mentioned, that both my list are reading from the computer directory/server, and are in huge amount, will it works?


I can remember that both my list have this similar code that I have inputted:
Code: Select all
setJobs = str(self.jobName.text())
         jobs = ivygate.Job.getItems({'job': '%s'%show})
            self.jobShotsList.addItems(jobs)


Basically, jobName is a QLineEdit where user input the name of Job and it retrieves all the folders names from the server which is shown in the second line of the code. As such, for it to display all the info in the ListWidget, I write in the last line of code. The coding above is similar to the one for mainList as well except the directory is more like /user_data/%s %setJobs

And what happens if I keep changing the refList
salik89
 
Posts: 13
Joined: Mon Jan 20, 2014 4:36 pm

Re: How to tally and displays items in QListWidget

Postby salik89 » Thu Jan 23, 2014 4:12 am

Hi Yoriz, your code helps me and it work fine.
Thanks alot. As my for latter part of the questions, it is working as well.

However I have a questions and that is filtering..
Is it possible for me to use the same method if I am to type in a keyword, using the same example, say I type in 'T01' into a QLineEdit, and both the mainList and refList will displays just only the T01?
salik89
 
Posts: 13
Joined: Mon Jan 20, 2014 4:36 pm

Filtering and tallying items

Postby salik89 » Thu Jan 23, 2014 10:20 am

Hiya all, I ahve 2 questions.

1. I have 2 lists (QListWidget) which contains an amount of items that I would like to implement a function where as the user types in a keyword (definetly in the list) into the QLineEdit and via a QPushButton and it will filters out both the list and displays the same contents as the keyword in both list

Additionally, I am curious to know if user will to select just an item from the QListWidget and I would like to make it the selected item to tally/check against another list, can I write it as:
Code: Select all
    if List01.currentItem = List02:
            print("Item is also in List 02")
    else:
            print("Item is not in List 02")
salik89
 
Posts: 13
Joined: Mon Jan 20, 2014 4:36 pm

Re: Filtering and tallying items

Postby Kebap » Thu Jan 23, 2014 11:54 am

Hiya salik,

as to your second question, no you can't write like "if Item = List02:" because an item will never be equal to a list. What were you thinking? But you can write a very similar way for your case: "if Item in List02". This will do the trick!

For your first question, please show some code where you try, and what goes wrong there. Thanks

salik89 wrote:Hiya all, I ahve 2 questions.

1. I have 2 lists (QListWidget) which contains an amount of items that I would like to implement a function where as the user types in a keyword (definetly in the list) into the QLineEdit and via a QPushButton and it will filters out both the list and displays the same contents as the keyword in both list

Additionally, I am curious to know if user will to select just an item from the QListWidget and I would like to make it the selected item to tally/check against another list, can I write it as:
Code: Select all
    if List01.currentItem = List02:
            print("Item is also in List 02")
    else:
            print("Item is not in List 02")

Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 396
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: How to tally and displays items in QListWidget

Postby Kebap » Thu Jan 23, 2014 12:12 pm

This thread seems to continue there:

Edit: link removed, topics merged. thanks
Last edited by Yoriz on Thu Jan 23, 2014 12:53 pm, edited 1 time in total.
Reason: removed link to the topic that is now merged
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 396
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: How to tally and displays items in QListWidget

Postby salik89 » Thu Jan 23, 2014 1:58 pm

Hi Kebap, I will try out your method for my second case.

Well and as for my first qns, I'm not sure if this is right but here it goes.
PushBtn = QPushButton()
LineEdit = QLineEdit()
mainList = QListWidget()
refList = QListWidget()

This is my code that Yoriz has provided for me and I am able to implement it, thanks to his help! :)

Code: Select all
    def fillrefList(self):
        show = str(self.LineEdit.text())
        localDir =  os.listdir("/user_data/Test/")
        jobList = os.listdir("/jobs/ComputeTest/")
               
           items = []
           for item in jobList:
               if item in localDir:
                   items.append(item)
     
        self.refList.clear()
        self.refList.addItems(items)


So using back the example I stated in my first thread, the output will be
Code: Select all
mainList:
[T01][T02][T03][T04][T05]

refList:
[T01][T02][T03]


As mentioned, I am trying to implement in a function where user types in say, T01 into the LineEdit, both the lists will only shows T01 upon the click of PushButton
Code: Select all
def filterList(self):
text = self.LineEdit.text()
filter[]
if text in mainList:
    if text in refList:
       filter.append(text)
self.refList.clear()
self.mainList.clear()
self.refList.addItems(filter)
self.mainList.addItems(items)


I feel that this part looks very weird as I am trying to implement the previous coding as I presume that it will somewhat works the same way... Can someone guide me?
salik89
 
Posts: 13
Joined: Mon Jan 20, 2014 4:36 pm


Return to GUI

Who is online

Users browsing this forum: Google Feedfetcher and 2 guests