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.