pythonpyqt5sysqtcoreqtwidgets

How to use pyqt to achieve the effect of Select-Option addition and deletion


I want to use QT to achieve the effect of Select-Option, support deleting Option by clicking the close button,this is my expected rendering: enter image description here

The following is my demo. I want to create a custom Widget insert into the QListWidget to achieve this effect, but the logic of closing the Option seems to be not easy to achieve. Or am I thinking this way completely wrong? enter image description here

from PyQt5 import QtCore
from PyQt5 import QtWidgets


class WID(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(WID, self).__init__(parent)
        self.setMaximumSize(QtCore.QSize(99, 43))
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.label = QtWidgets.QLabel(self)
        self.label.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.label.setStyleSheet("border:none;")
        self.label.setObjectName("label")
        self.horizontalLayout.addWidget(self.label)
        self.toolButton = QtWidgets.QToolButton(self)
        self.toolButton.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.toolButton.setObjectName("toolButton")
        self.horizontalLayout.addWidget(self.toolButton)
        self.horizontalLayout_2.addLayout(self.horizontalLayout)
        self.label.setText("1234")
        self.toolButton.setText("x")
        

class ListWidget(QtWidgets.QListWidget):
    def __init__(self, parent=None):
        super(ListWidget, self).__init__(parent)
        self.verticalLayout = QtWidgets.QVBoxLayout(self)
        self.setFlow(QtWidgets.QListView.LeftToRight)
        self.setResizeMode(QtWidgets.QListView.Fixed)

        for i in range(0, 3):
            wid = WID()
            item = QtWidgets.QListWidgetItem(self)
            item.setSizeHint(QtCore.QSize(100, 100))
            self.setItemWidget(item, wid)

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    listWidget = ListWidget()
    listWidget.show()
    sys.exit(app.exec_())

Solution

  • I use the QHBoxLayout::addWidget method to add widgets, and use the deleteLater solt to delete the QWidget:

    from PyQt5 import QtCore
    from PyQt5 import QtWidgets
    
    class WID(QtWidgets.QWidget):
        def __init__(self, parent=None):
            super(WID, self).__init__(parent)
            self.setMaximumSize(QtCore.QSize(99, 43))
            self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self)
            self.horizontalLayout_2.setObjectName("horizontalLayout_2")
            self.horizontalLayout = QtWidgets.QHBoxLayout()
            self.horizontalLayout.setObjectName("horizontalLayout")
            self.label = QtWidgets.QLabel(self)
            self.label.setLayoutDirection(QtCore.Qt.LeftToRight)
            self.label.setStyleSheet("border:none;")
            self.label.setObjectName("label")
            self.horizontalLayout.addWidget(self.label)
            self.toolButton = QtWidgets.QToolButton(self)
            self.toolButton.setLayoutDirection(QtCore.Qt.LeftToRight)
            self.toolButton.setObjectName("toolButton")
            self.horizontalLayout.addWidget(self.toolButton)
            self.horizontalLayout_2.addLayout(self.horizontalLayout)
            self.label.setText("1234")
            self.toolButton.setText("x")
            self.toolButton.clicked.connect(self.deleteLater)
    
    class ListWidget(QtWidgets.QWidget):
        def __init__(self, parent=None):
            super(ListWidget, self).__init__(parent)
            self.setObjectName("self")
            self.resize(437, 29)
            self.setTabletTracking(False)
            self.setLayoutDirection(QtCore.Qt.LeftToRight)
            self.setStyleSheet("background-color: rgb(255, 255, 255);")
            self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self)
            self.horizontalLayout_2.setSizeConstraint(QtWidgets.QLayout.SetDefaultConstraint)
            self.horizontalLayout_2.setContentsMargins(-1, 0, 0, 0)
            self.horizontalLayout_2.setObjectName("horizontalLayout_2")
    
            QtCore.QMetaObject.connectSlotsByName(self)
            
            self.horizontalLayout_2.setAlignment(QtCore.Qt.AlignLeft)
    
            for i in range(0, 3):
                self.addWidget()
    
        def addWidget(self):
            self.horizontalLayout_2.addWidget(WID())
    
    
    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
        listWidget = ListWidget()
        listWidget.show()
        sys.exit(app.exec_())