0

I am very new to PyQt5 and I'm trying to make a custom qlistwidgetitem for a qlistwidget in python3+ but so far with no success.

The original code which I like and tried to port is from this link. Unfortunately it's in PyQt4:

PyQt QListWidget custom items

import sys
from PyQt4 import QtGui

class QCustomQWidget (QtGui.QWidget):
    def __init__ (self, parent = None):
        super(QCustomQWidget, self).__init__(parent)
        self.textQVBoxLayout = QtGui.QVBoxLayout()
        self.textUpQLabel    = QtGui.QLabel()
        self.textDownQLabel  = QtGui.QLabel()
        self.textQVBoxLayout.addWidget(self.textUpQLabel)
        self.textQVBoxLayout.addWidget(self.textDownQLabel)
        self.allQHBoxLayout  = QtGui.QHBoxLayout()
        self.iconQLabel      = QtGui.QLabel()
        self.allQHBoxLayout.addWidget(self.iconQLabel, 0)
        self.allQHBoxLayout.addLayout(self.textQVBoxLayout, 1)
        self.setLayout(self.allQHBoxLayout)
        # setStyleSheet
        self.textUpQLabel.setStyleSheet('''
            color: rgb(0, 0, 255);
        ''')
        self.textDownQLabel.setStyleSheet('''
            color: rgb(255, 0, 0);
        ''')

    def setTextUp (self, text):
        self.textUpQLabel.setText(text)

    def setTextDown (self, text):
        self.textDownQLabel.setText(text)

    def setIcon (self, imagePath):
        self.iconQLabel.setPixmap(QtGui.QPixmap(imagePath))

class exampleQMainWindow (QtGui.QMainWindow):
    def __init__ (self):
        super(exampleQMainWindow, self).__init__()
        # Create QListWidget
        self.myQListWidget = QtGui.QListWidget(self)
        for index, name, icon in [
            ('No.1', 'Meyoko',  'icon.png'),
            ('No.2', 'Nyaruko', 'icon.png'),
            ('No.3', 'Louise',  'icon.png')]:
            # Create QCustomQWidget
            myQCustomQWidget = QCustomQWidget()
            myQCustomQWidget.setTextUp(index)
            myQCustomQWidget.setTextDown(name)
            myQCustomQWidget.setIcon(icon)
            # Create QListWidgetItem
            myQListWidgetItem = QtGui.QListWidgetItem(self.myQListWidget)
            # Set size hint
            myQListWidgetItem.setSizeHint(myQCustomQWidget.sizeHint())
            # Add QListWidgetItem into QListWidget
            self.myQListWidget.addItem(myQListWidgetItem)
            self.myQListWidget.setItemWidget(myQListWidgetItem, myQCustomQWidget)
        self.setCentralWidget(self.myQListWidget)

app = QtGui.QApplication([])
window = exampleQMainWindow()
window.show()
sys.exit(app.exec_())

Any help is welcome.

1 Answers1

1

Differences Between PyQt4 and PyQt5 https://docs.huihoo.com/pyqt/PyQt5/pyqt4_differences.html#differences-between-pyqt4-and-pyqt5

import sys
#from PyQt4 import QtGui
from PyQt5 import QtGui, QtWidgets


class QCustomQWidget (QtWidgets.QWidget):                       # QtWidgets
    def __init__ (self, parent = None):
        super(QCustomQWidget, self).__init__(parent)
        self.textQVBoxLayout = QtWidgets.QVBoxLayout()          # QtWidgets
        self.textUpQLabel    = QtWidgets.QLabel()               # QtWidgets
        self.textDownQLabel  = QtWidgets.QLabel()               # QtWidgets
        self.textQVBoxLayout.addWidget(self.textUpQLabel)
        self.textQVBoxLayout.addWidget(self.textDownQLabel)
        self.allQHBoxLayout  = QtWidgets.QHBoxLayout()          # QtWidgets
        self.iconQLabel      = QtWidgets.QLabel()               # QtWidgets
        self.allQHBoxLayout.addWidget(self.iconQLabel, 0)
        self.allQHBoxLayout.addLayout(self.textQVBoxLayout, 1)
        self.setLayout(self.allQHBoxLayout)
        # setStyleSheet
        self.textUpQLabel.setStyleSheet('''
            color: rgb(0, 0, 255);
        ''')
        self.textDownQLabel.setStyleSheet('''
            color: rgb(255, 0, 0);
        ''')

    def setTextUp (self, text):
        self.textUpQLabel.setText(text)

    def setTextDown (self, text):
        self.textDownQLabel.setText(text)

    def setIcon (self, imagePath):
        self.iconQLabel.setPixmap(QtGui.QPixmap(imagePath).scaled(60, 60))   # + .scaled(60, 60

class exampleQMainWindow (QtWidgets.QMainWindow):                            # QtWidgets
    def __init__ (self):
        super(exampleQMainWindow, self).__init__()
        # Create QListWidget
        self.myQListWidget = QtWidgets.QListWidget(self)                     # QtWidgets
        for index, name, icon in [
            ('No.1', 'Meyoko',  'lena-2.png'),
            ('No.2', 'Nyaruko', 'im.png'),
            ('No.3', 'Louise',  'Ok.png')]:
            # Create QCustomQWidget
            myQCustomQWidget = QCustomQWidget()
            myQCustomQWidget.setTextUp(index)
            myQCustomQWidget.setTextDown(name)
            myQCustomQWidget.setIcon(icon)
            # Create QListWidgetItem
            myQListWidgetItem = QtWidgets.QListWidgetItem(self.myQListWidget)  # QtWidgets
            # Set size hint
            myQListWidgetItem.setSizeHint(myQCustomQWidget.sizeHint())
            # Add QListWidgetItem into QListWidget
            self.myQListWidget.addItem(myQListWidgetItem)
            self.myQListWidget.setItemWidget(myQListWidgetItem, myQCustomQWidget)
        self.setCentralWidget(self.myQListWidget)


if __name__ == '__main__':
    app = QtWidgets.QApplication([])                                           # QtWidgets
    window = exampleQMainWindow()
    window.show()
    sys.exit(app.exec_())

enter image description here

S. Nick
  • 12,879
  • 8
  • 25
  • 33
  • myQListWidgetItem = QtWidgets.QListWidgetItem(self.myQListWidget) This is the only line I don't understand. Does this instantiate the item as a list, a child of the list, or something else? – Michael Effraimidis Feb 05 '20 at 12:37
  • @MichaelEffraimidis https://doc.qt.io/qt-5/qlistwidgetitem.html#details – S. Nick Feb 05 '20 at 12:58
  • I've successfully created a custom widget list. How do I go on about deleting it in an efficient way (the list is dynamic)? I try the myQListWidget.removeItemWidget(myQCustomQWidget) but it drops error " argument 1 has unexpected type 'myQCustomQWidget'. Do I have to keep and use the myQListWidgetItem variable or is there a more efficient way? – Michael Effraimidis Feb 06 '20 at 15:55
  • @MichaelEffraimidis If you have other problems, you should create a new question. – S. Nick Feb 06 '20 at 16:02