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.
Q, it is confusing - this is the only way to callQtclasses. 2) Code withself.QListWidget.very strange, surely not mistaken anywhere? Is this the name of the field that you named? 3)self.item =andself.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? - gil9redself.itemsomewhere else working or only in thelistWdigetFillmethod? - gil9red