Removing an item from a QListView in PyQt

S

Svenn Bjerkem

Hi,

I am looking for a bit more elaboration on the problem of deleting
elements from a QListsView. I know this is a tricky problem with
references, but I have not been able to extract enough knowledge from
the documentation to solve a specific problem:

In a dialog I have a QListView called referenceList. Entries are added
and deleted from this list with buttons addButton and removeButton
connected with sockets:

----
class IdeaInputBase(QDialog):
def __init__(self,parent = None,name = None,modal = 0,fl = 0):
QDialog.__init__(self,parent,name,modal,fl)


self.connect(self.addButton,SIGNAL("clicked()"),self.addNewReferenceSlot)

self.connect(self.removeButton,SIGNAL("clicked()"),self.removeReferenceSlot)
....
----
class IdeaInput(IdeaInputBase):
def __init__(self):
IdeaInputBase.__init__(self)

def addNewReferenceSlot(self):
self.referenceList.insertItem(QListViewItem(self.referenceList, "An
Item"))

def removeReferenceSlot(self):
print "selected has address %s" % (self.referenceList.selectedItem())
self.referenceList.removeChild(self.referenceList.selectedItem())
----
Adding Items by pressing the addButton works fine. I then select one of
the list elements and press the removeButton. This is the output I get.

selected has address <qt.QListViewItem object at 0x1c98f0>
Traceback (most recent call last):
File "./idea_input.py", line 20, in removeReferenceSlot
self.referenceList.removeChild(self.referenceList.selectedItem())
TypeError: argument 1 of QScrollView.removeChild() has an invalid type

I see that I have an address of a QListViewItem, but I am not able to
delete it. What am I obviously doing wrong here?

Kind regards,
 
D

David Boddie

Svenn Bjerkem wrote: > In a dialog I have a QListView called
referenceList. Entries are added > and deleted from this list with
buttons addButton and removeButton > connected with sockets: [...] >
def removeReferenceSlot(self): > print "selected has address %s" %
(self.referenceList.selectedItem()) >
self.referenceList.removeChild(self.referenceList.selectedItem()) >
---- > Adding Items by pressing the addButton works fine. I then select
one of > the list elements and press the removeButton. This is the
output I get. > > selected has address <qt.QListViewItem object at
0x1c98f0> > Traceback (most recent call last): > File
"./idea_input.py", line 20, in removeReferenceSlot >
self.referenceList.removeChild(self.referenceList.selectedItem()) >
TypeError: argument 1 of QScrollView.removeChild() has an invalid type
That's because removeChild() is a QScrollView method. You can call this
method because QListView inherits it from QScrollView, but it's not the
method you're looking for. > I see that I have an address of a
QListViewItem, but I am not able to > delete it. What am I obviously
doing wrong here? You need to call your QListView instance's
takeItem() method with the item you want to remove as the argument.
Hope this helps, David
 
D

David Boddie

To summarize my previous misformatted post: removeChild() is a
QScrollView method. You can call it because QListView inherits it
from QScrollView, but you need to call your QListView instance's
takeItem() method with the item you want to remove as the argument.


Hope this helps,

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top