clicked() signal for QListView in PyQt4

2019-02-28 15:27发布

问题:

I have a working QListView, but from the documentation, I can't figure out how to get a signal to fire with the index of the newly selected item. Any ideas?

回答1:

Imho, an easier way to achieve this would be to use a QListWidget instead of a QListView, this way you could use the itemClicked signal, which sends the selected item to the callback function.



回答2:

These is a snipplet of code of how I achieved it:

class VenueList(QListView):
    def __init__(self, parent, venues):
        super(VenueList, self).__init__(parent)
        self.clicked.connect(self.venue_selected)
        [...]

    def venue_selected(self, index):
        venue = self.model().data(index, VenueListModel.VenueRole)
        doStuff()

You can browse the full code of how I used this here (line 69). I do warn you, however, that this code is pretty bad, and needs some serious refactoring.