0

I am using a QListWidget whose items can be edited by double-clicking (with item.setFlags(item.flags() | Qt.ItemIsEditable)). I would like to check if the name entered by the user is correct (I would check if it corresponds to a correct variable name using isidentifier() and if it isn't a Python keyword with iskeyword()). I'd rather not use a QListView because it would require a lot of changes in my code.

Below is an example:

from PyQt5.QtWidgets import QListWidget, QListWidgetItem
from PyQt5.QtCore import Qt


class MyListWidget(QListWidget):
    def __init__(self):
        super().__init__()

        item1 = QListWidgetItem('item 1')
        item2 = QListWidgetItem('item 2')

        item1.setFlags(item1.flags() | Qt.ItemIsEditable)
        item2.setFlags(item2.flags() | Qt.ItemIsEditable)

        self.addItem(item1)
        self.addItem(item2)

        self.show()


if __name__ == '__main__':
    from PyQt5.QtWidgets import QApplication
    import sys


    app = QApplication(sys.argv)
    win = MyListWidget()
    sys.exit(app.exec_())

The items can be changed to any value, even a string containing special characters or an empty string.

hug
  • 137
  • 1
  • 11
  • Consider [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Matho Jun 16 '17 at 20:19
  • You can try the [following method](http://www.qtforum.org/article/20288/validating-data-input-for-qlistwidget.html). I think you don't need to switch to `QListView` for it. Just call `setItemDelegate`. See also https://stackoverflow.com/questions/26614678/validating-user-input-in-a-qtableview – m7913d Jun 20 '17 at 17:31

0 Answers0