In order to do such a circular list, you can subclass QListWidget
, override its keyPressEvent()
method and check whether you are in the first/last row before moving via setCurrentRow()
if needed.
class CircularListWidget(QtGui.QListWidget):
"""
Circular ListWidget.
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Down:
if self.currentRow() == self.count()-1:
self.setCurrentRow(0)
return
elif event.key() == QtCore.Qt.Key_Up:
if self.currentRow() == 0:
self.setCurrentRow(self.count()-1)
return
# Otherwise, parent behavior
super().keyPressEvent(event)
Just replace your QListWidget
by this CircularListWidget
and it should work. I did not find any built-in method to do what you ask for, hence this workaround.