0

I try to add multiple widgets in one listitem. I have a listwidget with listitems. The listitems contains an icon and text. I try to get the filename of the icon back instead of the text and want to add a button and a lineedit to the same item. So you have a icon with a button and a text field in place of the default text (under the icon). I can't find usefull examples and nothing I tried worked so Is something possible? Or am I using the wrong widget for this? I'm a noob at python/pyqt.

def additems(self):

    image = QtGui.QPixmap("C:/Users/Gebruiker/Pictures/jack.jpg")

    for i in range(10):
        icon = QtWidgets.QListWidgetItem(QtGui.QIcon(image), "Item " + str(i))
        self.listWidget.addItem(icon)

        item = QtWidgets.QListWidgetItem()

        line = QtWidgets.QLineEdit(textChanged = self.textChanged)

        button = QtWidgets.QPushButton("Button " + str(i), clicked=self.buttonClicked)

        cellwidget = QtWidgets.QWidget()
        layout = QtWidgets.QHBoxLayout()

        layout.addWidget(line)
        layout.addWidget(button)

        cellwidget.setLayout(layout)
        cellwidget.show() # This is showing the widget with the line and button, but it open en close the window 10(Range of for loop) times. Widget disapears when resizing window.

        self.listWidget.setItemWidget(icon, cellwidget)

Edit: Updatet the code.

Michael
  • 3
  • 5
  • Create a QWidget as a container, set a layout for it, add your widgets to that layout, and finally use `setItemWidget()` with the container widget. – musicamante Mar 06 '22 at 00:09
  • @musicamante ` self.listWidget.setItemWidget(icon, hbox) TypeError: setItemWidget(self, QListWidgetItem, QWidget): argument 2 has unexpected type 'QHBoxLayout'` – Michael Mar 06 '22 at 00:55
  • Please read more carefully what I wrote: `setItemWidget()` must be used with the container widget, not a layout. Read the duplicate answer. – musicamante Mar 06 '22 at 00:57
  • Sorry, like I said I just begon with this. I forgot the QWidget. The Qwidget works, but when I set it to setItemWidget, it's not shown. It is shown when I add to other layout. Can't paste the code here. EDIT: I added cellwidget.show(). But now it's opening and closing the window for the amount of icons (Like 100) and if I resize the window the Qwidget is gone. – Michael Mar 06 '22 at 01:27
  • Without knowing what you did, it's impossible to understand what is the problem, I suggest you to open another question. – musicamante Mar 06 '22 at 01:33
  • @musicamante I understand. I changed my topic with the recent code. Also the tries to get the information back op the button and line without success. – Michael Mar 06 '22 at 01:46
  • that code alone is insufficient to understand the problem, please provide a [mre]. – musicamante Mar 06 '22 at 12:08
  • Sorry but that is *not* a MRE. We need something we could easily copy, paste and run, possibly without any substantial modification. – musicamante Mar 08 '22 at 03:22

0 Answers0