如何使用pyqt实现Select-选项增删改查的效果
How to use pyqt to achieve the effect of Select-Option addition and deletion
我想用QT实现Select-Option的效果,支持点击关闭按钮删除Option,这是我预期的效果图:
下面是我的demo。我想创建一个自定义的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_())
我使用QHBoxLayout::addWidget
方法添加widgets,使用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_())
我想用QT实现Select-Option的效果,支持点击关闭按钮删除Option,这是我预期的效果图:
下面是我的demo。我想创建一个自定义的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_())
我使用QHBoxLayout::addWidget
方法添加widgets,使用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_())