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_())
setLayoutmethod, you need to create a layout (if the widgets need to be vertically arranged, thenQVBoxLayout), and throughlayout.addWidgetadd widgets (layout.addWidget(button)), then call the widgetsetLayout(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