I am running the below code:
self.myList = QListWidget()
for i in range(3):
self.Item = QListWidgetItem()
self.name = 'A'+'%04d'% i
self.Item.setText(self.name)
self.myList.addItem(self.Item)
print self.myList.selectedItems()
it prints an empty list:
[]
Please suggest where I am missing.
According to QT docs (i've used c++ ones, but it's ok for python bridge as well),
QList<QListWidgetItem *> QListWidget::selectedItems () const
Returns a list of all selected items in the list widget.
This means, that print self.myList.selectedItems() will output all the list of items user was selected, not all the items are in widget.
You can try to use count() to get number of items and item(number) to get one.
Related
For the program that I am required to write up, I need the name of a Listbox item to also be the text in the label. This is the code that I thought would work, but it only configurated the label to be the item's number in the Listbox, not the name:
def openAccount(self):
selectedItem = ltboxSrcUser.curselection()
for item in selectedItem:
rootMainPage.withdraw()
rootOthAccPage.deiconify()
lblOtherUserName.config(text = ltboxSrcUser.curselection())
Can anybody help me out?
The documentation for the listbox shows that curselection returns the index of the selected items, not the values. To get the values you must pass the index to the get method.
def openAccount(self):
selectedItems = ltboxSrcUser.curselection()
if selectedItems:
rootMainPage.withdraw()
rootOthAccPage.deiconify()
index = selectedItems[0]
item = ltboxSrcUser.get(index)
lblOtherUserName.config(text=item)
I need some help with deleting some items from some lists at the same time, when a button is clicked.
This is the code:
class Window(QMainWindow):
list_1 = [] #The items are strings
list_2 = [] #The items are strings
def __init__(self):
#A lot of stuff in here
def fillLists(self):
#I fill the lists list_1 and list_2 with this method
def callAnotherClass(self):
self.AnotherClass().exec_() #I do this to open a QDialog in a new window
class AnotherClass(QDialog):
def __init__(self):
QDialog.__init__(self)
self.listWidget = QListWidget()
def fillListWidget(self):
#I fill self.listWidget in here
def deleteItems(self):
item_index = self.listWidget.currentRow()
item_selected = self.listWidget.currentItem().text()
for i in Window.list_2:
if i == item_selected:
?????????? #Here is where i get confussed
When i open the QDialog with a combination of keys, i see in the QListWidget some items. In the deleteItems method, i get the index and the text from the item that i selected in the QListWidget. That works fine.
What i need to do is to delete the item from the list_1, list_2, and the QListWidget when i press a button (that i have already created).
How can i do this? Hope you can help me.
Python lists have a "remove" object that perform that action directly:
Window.list_2.remove(item_selected)
(with no need for your for loop)
If you ever need to perform more complex operations on the list items, you can retrieve an item's index with the index method instead:
position = Window.list_2.index(item_selected)
Window.list_2[position] += "(selected)"
And in some ocasions you will want to do a for loop getting to the actual index, as well as the content at that index of a list or other sequence. In that case, use the builtin enumerate.
Using the enumerate pattern, (if removedid not exist) would look like:
for index, content in enumerate(Window.list_2):
if content == item_selected:
del Window.list_2[index]
# must break out of the for loop,
# as the original list now has changed:
break
if you have the value, then just find its index in each list and then delete it. Something like:
item_selected = self.listWidget.currentItem().text()
i = Window.list_2.index(item_selected)
if i >= 0:
del Window.list_2[i]
You can also use directly Widow.list_x.remove(value) but it can throw an exception if the value does not exist.
Im learning how to use QTreeWidget and Im stuck adding new items to it. The QTreewidget itself is created with qtdesigner, so my idea was just to add items. eg:
tw = self.ui.treeWidget
item = QtGui.QTreeWidgetItem("TEST")
tw.addTopLevelItem(item)
But in the treewidget only appears the first letter of "TEST". Doesnt matter what I type, it always only displays the first letter and I have no idea why...
QTreeWidgetItem constructor expects a list of strings. Try this:
tw = self.ui.treeWidget
item = QtGui.QTreeWidgetItem(["TEST"])
tw.addTopLevelItem(item)
The QtGui.QTreeWidgetItem is expecting a list for different columns. You can simply wrap your text in a list
item = QtGui.QTreeWidgetItem(["TEST"])
or you can set the text for a specific column.
item = QtGui.QTreeWidgetItem()
item.setText(0, "TEST")
Created a QtGui.QListWidget list widget:
myListWidget = QtGui.QListWidget()
Populated this ListWidget with QListWidgetItem list items:
for word in ['cat', 'dog', 'bird']:
list_item = QtGui.QListWidgetItem(word, myListWidget)
Now connect a function on list_item's left click:
def print_info():
print myListWidget.currentItem().text()
myListWidget.currentItemChanged.connect(print_info)
As you see from my code all I am getting on a left click is a list_item's label name. But aside from a label name I would like to get a list_item's index number (order number as it is displayed in ListWidget). I would like to get as much info on left-clicked list_item as possible. I looked at dir(my_list_item). But I can't anything useful there ( other than already used my_list_item.text() method which returns a list_item's label name). Thanks in advance!
Use QListWidget.currentRow to get the index of the current item:
def print_info():
print myListWidget.currentRow()
print myListWidget.currentItem().text()
A QListWidgetItem does not know its own index: it's up to the list-widget to manage that.
You should also note that currentItemChanged sends the current and previous items as arguments, so you could simplify to:
def print_info(current, previous):
print myListWidget.currentRow()
print current.text()
print current.isSelected()
...
Well, I have listed some of the things you can display about the current item, if you want more than this then you should look through the PyQt Documentation. link
def print_info():
print myListWidget.currentItem().text()
print myListWidget.row(myListWidget.currentItem())
print myListWidget.checkState() # if it is a checkable item
print myListWidget.currentItem().toolTip().toString()
print myListWidget.currentItem().whatsThis().toString()
myListWidget.currentItemChanged.connect(print_info)
i am currently designing a QT-gui in Python and i want to allow the user to switch QListWidgetItems between two QListWidgets. Multiple selection (CTRL) is allowed and switching is done by two control buttons.
In the QT4-Designer the lists looks like this
So if the user selects for example two items from the left list and clicks on the '>' Button the items have to be added to the right list and consequently deleted from the left list.
My current triggered Button-Events look like this:
def switchR( self ):
itemlistSel = self.list_left.selectedItems()
for item in itemlistSel:
self.list_right.addItem( item )
self.list_left.removeItemWidget( item )
But nothing happens? Someone got a quick solution?
The removeItemWidget() method doesn't quite do what you're expecting it to do (see docs). Use takeItem(), addItem() and row() instead:
def switch(self):
items = self.left.selectedItems()
for item in items:
n = self.left.row(item) # get the index/row of the item
i = self.left.takeItem(n) # pop
self.right.addItem(i) # add to right QListWidget