In my program,I want to create a list (QListWidget) with modified list items (inherit QListWidgetItem) using Pyside6. The following code wants to modify the items so that they can show two images. But it is just an example, it is more how you create modified list in general that I want to know how to make.
My idea is:
- To create a class that inherits QListWidgetItem.
- In that class to create the layout with whatever widgets you want the listItem to have.
- Create a Widget that wraps this list item and then set that as the Item to be shown.
My code is just showing a blank MainWindow where I can highlight two rows meaning the list is created and the items is added, just not visible. "image1.jpg" and "image2.jpg" are just arbitrary.
import sys
from PySide6 import QtWidgets
from mainWindow import MainWindow
from PySide6.QtWidgets import QMainWindow, QHBoxLayout, QVBoxLayout, QListWidget, QListWidgetItem, QLabel, QWidget, QStyledItemDelegate
from PySide6.QtGui import QPixmap
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Rescue Creator")
self.mainLayout = QVBoxLayout()
self.listWidget = QListWidget()
item1 = listWidgetItem()
item2 = listWidgetItem()
self.listWidget.addItem(item1)
self.listWidget.addItem(item2)
self.setCentralWidget(self.listWidget)
class listWidgetItem(QListWidgetItem):
def __init__(self):
super().__init__()
self.imageLabel1 = QLabel()
self.imageLabel2 = QLabel()
self.image1 = QPixmap("image1.jpg")
self.image2 = QPixmap("image2.jpg")
self.imageLabel1.setPixmap(self.image1)
self.imageLabel2.setPixmap(self.image2)
self.listLayout = QHBoxLayout()
self.listLayout.addWidget(self.imageLabel)
self.listLayout.addWidget(self.layerMaskLabel)
self.widget = QWidget()
self.widget.setLayout(self.listLayout)
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
I tried the code above, chatGPT said something about a method like self.setLayout(self.listLayout) in the end of listWidgetItemClass but that might be for Pyqt5 or something.