Is it possible to somehow implement editing in QListWidget without opening additional windows as in this code. Those. when you clicked on a component in QListWidget , you could immediately change the text in it. I think you can do that when you click on an item , QLineEdit appeared in its place and there is editing in it, but it seems that this is not quite the best idea.

 import sys from PyQt5 import QtWidgets from PyQt5 import QtGui class MainWindwos(QtWidgets.QMainWindow): def __init__(self): super().__init__() self.setFont(QtGui.QFont('Times', 13)) self.setFixedSize(400, 600) self.listwidget = QtWidgets.QListWidget(self) self.listwidget.resize(400, 600) self.listwidget.addItem('привет') self.listwidget.itemClicked.connect(self.edit_item) def edit_item(self, item): self.dialog = QtWidgets.QDialog() self.dialog.setFont(QtGui.QFont('Times', 13)) self.edit = QtWidgets.QLineEdit(self.dialog) self.edit.resize(200, 30) self.push = QtWidgets.QPushButton(self.dialog) self.push.setText('сохранить') self.push.move(0, 50) self.push.clicked.connect(lambda: item.setText(self.edit.text())) self.push.clicked.connect(lambda: self.dialog.close()) self.dialog.setFixedSize(200, 200) self.dialog.show() if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) main = MainWindwos() main.show() sys.exit(app.exec_()) 

    1 answer 1

    It is necessary for the list items to indicate the ability to edit through the Qt.ItemIsEditable flag:

     import sys from PyQt5 import QtWidgets as qtw from PyQt5.QtCore import Qt class MainWindow(qtw.QMainWindow): def __init__(self): super().__init__() self.setWindowTitle('List Widget Edit') self.list_widget = qtw.QListWidget(self) self.setCentralWidget(self.list_widget) self.add_item('привет') self.add_item('hello') def add_item(self, text): item = qtw.QListWidgetItem(text) item.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsEditable) self.list_widget.addItem(item) if __name__ == "__main__": app = qtw.QApplication(sys.argv) main = MainWindow() main.show() sys.exit(app.exec_()) 

    When addItem("привет") was addItem("привет") it actually hid addItem(QListWidgetItem("привет")) , and the list items do not have an editing flag by default.


    An example with item.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsEditable) flags item.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsEditable) written for example, you can shorten it: item.setFlags(item.flags() | Qt.ItemIsEditable)


    The ability to edit elements is configured not only for each element individually, but also through the list widget via setEditTriggers . For example, this example will prohibit editing, although for elements it is allowed:

     self.list_widget.setEditTriggers(qtw.QListWidget.NoEditTriggers) 
    • And somehow, you can assign a change not when you click on the LMB but when you right-click, Ie. I have a context menu and there will be a button to edit (which, accordingly, it will have to do), and when I clicked on the LMB, it would QListWidget name in QListWidget - Twiss
    • 1) Add a context menu 2) When you click on an action in the menu, get the current item 3) Call programmatically editing for an item: doc.qt.io/archives/qt-4.8/qlistwidget.html#editItem . I think it will work, if it does not work, create a separate question :) - gil9red