[PySide] trying to mimic addressee field in email client

Frank Rueter | OHUfx frank at ohufx.com
Fri Mar 15 05:54:27 CET 2019

Hi all,

I am trying to mimic how the addressee widgets (“To” and “CC”) work in 
email clients but can’t get it right.
Upon valid input I would like to add a new row to teh list for 
additional input.
If the input is not found in the completer’s list O would like to keep 
the editor open and reset it to an empty value.

I am using a QListView with a delegate that opens a QLineEdit with a 
completer to check the input against a set list of strings.
With the below code I currently get “editing failed” errors when the 
input is incorrect (though the editor does stay open as desired).
With a valid input however the code adds two new rows instead of just one.

If somebody could have a look and offer some advise that would be awesome!


|import sys from PySide2 import QtWidgets, QtCore, QtGui class 
AddresseeModel(QtGui.QStandardItemModel): def __init__(self, w, 
parent=None): super(AddresseeModel, self).__init__(parent) def 
itemChanged(self, item): print item super(Model, self).itemChanged(item) 
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 = 
users + groups def createEditor(self, parent, option, index): 
self.editor = QtWidgets.QLineEdit(parent) 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: # this is called twice 
when input is valid self.is_valid.emit() else: 
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) model = 
AddresseeModel(list) self.setModel(model) item = QtGui.QStandardItem('') 
# add initial empty row model.appendRow(item) 
delegate = AddresseeDelegate(users, groups) 
delegate.is_not_valid.connect(self.keep_editor_open) 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 
keep_editor_open(self, index): '''Keep editor open at given index''' 
self.edit(index) if __name__ == '__main__': app = 
QtWidgets.QApplication(sys.argv) users = ['person A', 'person B', 
'person C'] groups = ['group A', 'group B', 'group C'] list = 
AddresseeListView(users, groups) list.show() list.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>* *

Your gateway to over 1,000 free tools... right inside of Nuke 

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

More information about the PySide mailing list