Add delete button for every item in QListView

How can I add a button to each item in the QListview that removes the onClick object? As shown in the following picture:

Item witch delete Button

EDIT: Since I am new to QT it would be nice to have some example to understand it better. And there seem to be three different Paths? What will be better? Use QAbstractItemView?

+3


source to share


2 answers


Yes. You will need to use:

QAbstractItemView::setIndexWidget ( const QModelIndex & index, QWidget * widget )

      



QListView inherits QAbstractItemView and when you try to customize list / tree views that usually look like place. Be careful though this doesn't scale very well without a delegate. Check out this thread: http://www.qtcentre.org/threads/26916-inserting-custom-Widget-to-listview

+1


source


You can also use a generic approach that can work with a variety of containers, including the underlying model of your list view.

Each list item has a signal requestRemoval(Item*)

and a slot removeMe()

, connect the X button to a slot removeMe()

in each element constructor, in removeMe()

you emit a signal requestRemoval(this)

that is connected to a slot removeHandler(Item*)

in your "parent" object when you create that item, which receives the item pointer that has the request deletion, and removes it from the container in use.



Basically, clicking the delete button causes the particular element to send the pointer itself to the parent delete handler, which deletes that entry.

EDIT: Please note that this is a general approach, as noted in the comments below, it can be applied without signals and slots, and even if it works, it is not the most efficient solution in your particular case.

0


source







All Articles