如何使用pyqt实现Select-Option添加删除的效果
我想用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:
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?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我使用
QHBoxLayout::addWidget
方法添加小部件,并使用deleteLater
solt 删除 QWidget:I use the
QHBoxLayout::addWidget
method to add widgets, and use thedeleteLater
solt to delete the QWidget: