I can not figure out why QListWidget does not change QListWidget although it displays to the console that this listwidget is changed in the listwidget . How to fix

 import sys from PyQt5 import QtCore from PyQt5 import QtWidgets from PyQt5 import QtGui from PyQt5 import QtCore class MainWindwos(QtWidgets.QMainWindow): def __init__(self): super().__init__() self.asd = Click() 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.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) self.listwidget.customContextMenuRequested.connect(self.asd.edit_item) class Click: def edit_item(self, item): self.main = MainWindwos() g = self.main.listwidget.itemAt(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: g.setText(self.edit.text())) self.push.clicked.connect(lambda: print(g.text())) self.dialog.setFixedSize(200, 200) self.dialog.show() if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) main = MainWindwos() main.show() sys.exit(app.exec_()) 
  • Kst, why don't you use layouts? They do not need to think about the size of the widgets and their location, plus they can automatically change the position and size of the widgets. There are QVBoxLayout, QHBoxLayout, QGridLayout, QFormLayout, Also, it is recommended to show the dialogs through exec, not show - then the dialog will become modal. Your dialogue is very simple, so you can use the standard instead of yours : doc.qt.io/qt-5/qinputdialog.html#getText - gil9red
  • @ gil9red Himself without a clue why I do not use them. Just do not understand how they specifically work ( - Twiss
  • See, widgets have a setLayout method, you need to create a layout (if the widgets need to be vertically arranged, then QVBoxLayout ), and through layout.addWidget add widgets ( layout.addWidget(button)) , then call the widget setLayout(layout) . Everything. After that, the widgets will be as infused. There are exceptions for QMainWindow and QScrollArea - they operate with a widget inside themselves, so a widget is first created for them, setLayout is called for it, and then that widget is set on them. For QMainWindow, this is the setCentralWidget method - gil9red
  • Here is my example of working with linkers (layout): github.com/gil9red/SimplePyScripts/blob/master/bin2str/… . A level above there is a screenshot - gil9red
  • @ gil9red I will be grateful, thank you) - Twiss

1 answer 1

The point is self.main = MainWindwos() . Using it, every time you create a new window with its list, although you want to change the value in the old window.

In Click you need to send a link to the window with which it will work:

 import sys from PyQt5 import QtWidgets from PyQt5 import QtGui from PyQt5 import QtCore class MainWindwos(QtWidgets.QMainWindow): def __init__(self): super().__init__() self.asd = Click(self) 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.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) self.listwidget.customContextMenuRequested.connect(self.asd.edit_item) class Click: def __init__(self, main): self.main = main def edit_item(self, item): item = self.main.listwidget.itemAt(item) self.dialog = QtWidgets.QDialog() self.dialog.setFont(QtGui.QFont('Times', 13)) self.edit = QtWidgets.QLineEdit(self.dialog) self.edit.setText(item.text()) 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: print(item.text())) self.dialog.setFixedSize(200, 200) self.dialog.show() if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) main = MainWindwos() main.show() sys.exit(app.exec_())