如何使用pyqt实现Select-Option添加删除的效果

发布于 2025-01-16 12:36:32 字数 2125 浏览 1 评论 0原文

我想用QT实现Select-Option的效果,支持通过点击关闭按钮删除Option,这是我期望的效果:

以下是我的演示。我想创建一个自定义的Widget插入到QListWidget中来实现这个效果,但是关闭Option的逻辑似乎不太容易实现。 或者我的这种想法完全错误? 输入图片此处描述

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_())

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_())

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

始终不够爱げ你 2025-01-23 12:36:33

我使用 QHBoxLayout::addWidget 方法添加小部件,并使用 deleteLater solt 删除 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_())

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_())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文