[PySide] trying to mimic addressee field in email client

Cristián Maureira-Fredes Cristian.Maureira-Fredes at qt.io
Wed Mar 20 15:29:28 CET 2019

Hello Frank,

I haven't had time to find a workaround of your situation,
but at least I understand the origin of the issue.

editingFinished() will be trigger twice, once you Tab/Enter and once you change the focus of the QLineEdit.
This seems to be related to a really old Qt Bug: https://bugreports.qt.io/browse/QTBUG-40

Maybe you can find a workaround with another QLineEdit signal, like returnPressed().


From: PySide <pyside-bounces at qt-project.org> on behalf of Frank Rueter | OHUfx <frank at ohufx.com>
Sent: Friday, March 15, 2019 05:54
To: pyside at qt-project.org
Subject: [PySide] trying to mimic addressee field in email client

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 = QtWidgets.QCompleter(self.valid_item_texts)
        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

class AddresseeListView(QtWidgets.QListView):
    def __init__(self, users=[], groups=[], parent=None):
        '''Simple addressee widget.'''
        super(AddresseeListView, self).__init__(parent)
        model = AddresseeModel(list)

        item = QtGui.QStandardItem('') # add initial empty row

        delegate = AddresseeDelegate(users, groups)

    def add_and_edit(self):
        '''Add a new row and open editor'''
        print 'adding row'
        new_item = QtGui.QStandardItem('')

    def keep_editor_open(self, index):
        '''Keep editor open at given 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)


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

[http://nukepedia.com/images/nuBridge/logo/nuBridge_logo.png] <http://www.nukepedia.com/nubridge>

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

More information about the PySide mailing list