There is a self-written widget inherited from QtWidgets.QWidget, which in turn is used to populate a QListWidget. Sample code:

class QCustomQWidget (QtWidgets.QWidget): #Класс для создания итемов в списке. def __init__ (self, parent = None): super(QCustomQWidget, self).__init__(parent) self.textQHBoxLayout = QtWidgets.QHBoxLayout() self.host = QtWidgets.QLabel() self.port = QtWidgets.QLabel() self.textQHBoxLayout.addWidget(self.host) self.textQHBoxLayout.addWidget(self.port) self.allQHBoxLayout = QtWidgets.QHBoxLayout() self.allQHBoxLayout.addLayout(self.textQHBoxLayout, 1) self.setLayout(self.allQHBoxLayout) def setHost(self, text): self.host.setText(text) def setPort(self, text): self.port.setText(text) 

And a method to fill QListWidget with instances of this class (The method itself is already in another class):

 def PortsListFill(self, ports): for port in ports: item = QCustomQWidget() item.setPort(port['port']) item.setHost(port['name']) myQlistWidgetItem = QtWidgets.QListWidgetItem(self.PortList) myQlistWidgetItem.setSizeHint(item.sizeHint()) self.PortList.addItem(myQlistWidgetItem) self.PortList.setItemWidget(myQlistWidgetItem, item) 

The problem is that when calling the fill method from the build function when drawing the main window. The method works correctly, and when the same method is called from another method. QCustomWidget is drawn as a separate window, not as a QListWidget. Help please understand what the problem and fix it.

  • 1) Please do not name your widgets with the prefix Q , it is confusing - this is the only way to call Qt classes. 2) Code with self.QListWidget. very strange, surely not mistaken anywhere? Is this the name of the field that you named? 3) self.item = and self.myQlistWidgetItem = look suspicious. Because they are in a loop and that method can be called again, so the question is why should they be stored in the field of the current class and what will happen to the widgets that existed before in those variables? 4) How do you use the listWdigetFill method? - gil9red
  • The code is approximate, partially modified, the essence of the problem is important, which is that when calling the listWidgetFill method from different methods of the same class, I get a different result. - Pavel Barya
  • C self.item somewhere else working or only in the listWdigetFill method? - gil9red
  • No, but I refer to the elements of QListWidgetItem, in other methods - Pavel Barya
  • MB is just the problem. According to the setItemWidget description , the widget must be static, otherwise the setItemWidget method does not fit and you need to do it through delegates - gil9red

0