[PySide] QListView - make tab key behave like enter key

Frank Rueter | OHUfx frank at ohufx.com
Tue Mar 19 06:35:31 CET 2019


Hi all,

I have a simple QListView in which I would like to edit items by 
clicking into empty space, typing, then hitting either enter or tab.
I’d like the tab Key_Tab event to behave exactly like Key_Enter event, 
but it’s a bit stubborn.
When I hit tab while editing an item (i.e. editor is open), the first 
item in the list is selected.

I noticed that when hitting the tab key in an open editor only a 
QtCore.QEvent.ShortcutOverride is triggered.
It looks like I can simply react to that in my situation though I’m not 
sure if that is wise?!
If I do, how would I manually emit an event that looks like a Key_Enter 
event?

Otherwise, how can I avoid above behaviour for the tab key and make it 
behave like a regular key (I assume it’s trying to change focus in the 
focus chain but don’t know how to make it stop)?

Here is some example code to show my problem.

Cheers,
frank

|class AddresseeDelegate(QtWidgets.QItemDelegate): is_valid = 
QtCore.Signal() is_not_valid = QtCore.Signal(QtCore.QModelIndex) def 
__init__(self, users=[], groups=[], parent=None): 
super(AddresseeDelegate, self).__init__(parent) self.valid_item_texts = 
[u['name'] for u in users] + [g['code'] for g in groups] def 
createEditor(self, parent, option, index): self.editor = 
QtWidgets.QLineEdit(parent) self.editor.setMinimumWidth(100) completer = 
QtWidgets.QCompleter(self.valid_item_texts) 
completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive) 
completer.setCompletionMode(QtWidgets.QCompleter.InlineCompletion) 
self.editor.setCompleter(completer) 
self.editor.editingFinished.connect(lambda: self.validate_text(index)) 
return self.editor def setModelData(self, editor, model, index): 
model.setData(index, editor.text()) def validate_text(self, index=None): 
if self.editor.text() in self.valid_item_texts: # valid input 
self.commitData.emit(self.editor) self.closeEditor.emit(self.editor) 
self.is_valid.emit() else: # invalid input 
self.closeEditor.emit(self.editor) self.is_not_valid.emit(index) class 
AddresseeListView(QtWidgets.QListView): def __init__(self, users=[], 
groups=[], parent=None): '''Simple addressee widget.''' 
super(AddresseeListView, self).__init__(parent) 
self.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers) 
self.setFlow(QtWidgets.QListView.LeftToRight) 
self.setResizeMode(QtWidgets.QListView.ResizeMode.Adjust) 
self.setSpacing(10) self.setViewMode(QtWidgets.QListView.IconMode) 
#self.setWrapping(True) # delegate delegate = AddresseeDelegate(users, 
groups) self.setItemDelegate(delegate) 
delegate.is_not_valid.connect(self.delete_invalid_index) # model model = 
QtGui.QStandardItemModel() #model = Model() self.setModel(model) def 
event(self, event): if event.type() == QtCore.QEvent.ShortcutOverride: # 
need to emit a Key_Enter event here event.accept() return 
super(AddresseeListView, self).event(event) def mousePressEvent(self, 
event): self.add_and_edit() def add_and_edit(self): '''Add a new row and 
open editor''' print 'adding row' new_item = QtGui.QStandardItem('') 
self.model().appendRow(new_item) 
self.edit(self.model().indexFromItem(new_item)) def 
delete_invalid_index(self, index): '''Remove invalid entry''' 
self.model().removeRow(index.row()) if __name__ == '__main__': app = 
QtWidgets.QApplication(sys.argv) users = [{'name':'aa'}, {'name':'bb'}] 
groups = [{'code':'cc'}, {'code':'dd'}] w = AddresseeListView(users, 
groups) w.show() w.raise_() app.exec_() |

​
-- 

ohufxLogo 50x50 <http://www.ohufx.com> 	
	*vfx compositing <http://ohufx.com/compositing.html> | *workflow 
customisation and consulting <http://ohufx.com/customising.html>* *
		*<http://ohufx.com/compositing.html>*
<http://www.nukepedia.com/nubridge> 	
	

Your gateway to over 1,000 free tools... right inside of Nuke 
<http://www.nukepedia.com/nubridge>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20190319/7e0b6b9d/attachment.html>


More information about the PySide mailing list