从 QListView 中正确删除多选行
Remove multi-selected rows correctly from QListView
我在尝试从 QListView 中正确删除多个 selected 行时遇到问题。它可以正常工作 selecting 项目从上到下,但是当你 select 多行从下到上时,它会在视图中保留 selected 行之一。
我如何解决这个问题,它可以按照用户选择的任何顺序正确删除所有 selected 行?
示例:
按此顺序选择行给出了无效的解决方案:
Level5, Level3, Level2
仅删除 Level2
更改为 for items in self.listView.selectedIndexes():
与您不能从上到下 select 的情况相反。
from PyQt5 import QtCore, QtGui, QtWidgets
import sys,functools
class Window(QtWidgets.QMainWindow):
def __init__(self, parent = None):
super(Window,self).__init__(parent)
self.setCentralWidget(QtWidgets.QWidget(self))
self.setWindowTitle('GUI')
mainlayout = QtWidgets.QVBoxLayout()
#Information for widgets
self.items = {'Level1':1,'Level2':2,'Level3':3,'Level4':4,'Level5':5,'Level6':6}
#LineEdit1
self.button = QtWidgets.QPushButton()
self.button.setText('Remove Selected Items')
firstBox = QtWidgets.QHBoxLayout()
firstBox.addWidget(self.button)
#ListView
self.listView = QtWidgets.QListView()
self.listView.setSelectionMode(QtWidgets.QAbstractItemView.MultiSelection)
secondBox = QtWidgets.QVBoxLayout()
secondBox.addWidget(self.listView)
#Add Layouts
mainlayout.addLayout(firstBox)
mainlayout.addLayout(secondBox)
self.centralWidget().setLayout(mainlayout)
#Model
self.model = QtGui.QStandardItemModel()
self.listView.setModel(self.model)
self.button.clicked.connect(functools.partial(self.selecteditems,self.model))
self.fillModel(self.model)
def fillModel(self,model):
for level in self.items:
item = QtGui.QStandardItem(str(level))
model.appendRow(item)
def selecteditems(self,model):
if len(self.listView.selectedIndexes()) > 1:
for items in reversed(self.listView.selectedIndexes()):
model.takeRow(items.row()) #works correctly selecting items from top to bottom, but doesn't work when selecting bottom to top
def main():
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
您也可以只使用 reversed
和 sorted
:
def selecteditems(self,model):
if len(self.listView.selectedIndexes()) > 1:
for items in reversed(sorted(self.listView.selectedIndexes())):
model.takeRow(items.row())
我在尝试从 QListView 中正确删除多个 selected 行时遇到问题。它可以正常工作 selecting 项目从上到下,但是当你 select 多行从下到上时,它会在视图中保留 selected 行之一。
我如何解决这个问题,它可以按照用户选择的任何顺序正确删除所有 selected 行?
示例:
按此顺序选择行给出了无效的解决方案: Level5, Level3, Level2
仅删除 Level2
更改为 for items in self.listView.selectedIndexes():
与您不能从上到下 select 的情况相反。
from PyQt5 import QtCore, QtGui, QtWidgets
import sys,functools
class Window(QtWidgets.QMainWindow):
def __init__(self, parent = None):
super(Window,self).__init__(parent)
self.setCentralWidget(QtWidgets.QWidget(self))
self.setWindowTitle('GUI')
mainlayout = QtWidgets.QVBoxLayout()
#Information for widgets
self.items = {'Level1':1,'Level2':2,'Level3':3,'Level4':4,'Level5':5,'Level6':6}
#LineEdit1
self.button = QtWidgets.QPushButton()
self.button.setText('Remove Selected Items')
firstBox = QtWidgets.QHBoxLayout()
firstBox.addWidget(self.button)
#ListView
self.listView = QtWidgets.QListView()
self.listView.setSelectionMode(QtWidgets.QAbstractItemView.MultiSelection)
secondBox = QtWidgets.QVBoxLayout()
secondBox.addWidget(self.listView)
#Add Layouts
mainlayout.addLayout(firstBox)
mainlayout.addLayout(secondBox)
self.centralWidget().setLayout(mainlayout)
#Model
self.model = QtGui.QStandardItemModel()
self.listView.setModel(self.model)
self.button.clicked.connect(functools.partial(self.selecteditems,self.model))
self.fillModel(self.model)
def fillModel(self,model):
for level in self.items:
item = QtGui.QStandardItem(str(level))
model.appendRow(item)
def selecteditems(self,model):
if len(self.listView.selectedIndexes()) > 1:
for items in reversed(self.listView.selectedIndexes()):
model.takeRow(items.row()) #works correctly selecting items from top to bottom, but doesn't work when selecting bottom to top
def main():
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
您也可以只使用 reversed
和 sorted
:
def selecteditems(self,model):
if len(self.listView.selectedIndexes()) > 1:
for items in reversed(sorted(self.listView.selectedIndexes())):
model.takeRow(items.row())