Select QListView 中的所有项目并在目录更改时取消选择所有项目
Select all items in QListView and deselect all when directory is changed
我创建了一个 window 这样的 !当我 select 一个目录时,我想 select 正确的 Qlist 中的所有项目,当我切换到另一个目录时,我会 deselect 前一个目录中的项目和select 我当前目录中的所有项目。
我该如何解决这个问题?
要select和deselect所有项目你必须分别使用selectAll()
和clearSelection()
。但是 selection 必须在更新的视图之后,为此使用 layoutChanged
信号,而且 selection 模式必须设置为 QAbstractItemView::MultiSelection
.
import sys
from PyQt5 import QtCore, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
super(Widget, self).__init__(*args, **kwargs)
hlay = QtWidgets.QHBoxLayout(self)
self.treeview = QtWidgets.QTreeView()
self.listview = QtWidgets.QListView()
hlay.addWidget(self.treeview)
hlay.addWidget(self.listview)
path = QtCore.QDir.rootPath()
self.dirModel = QtWidgets.QFileSystemModel(self)
self.dirModel.setRootPath(QtCore.QDir.rootPath())
self.dirModel.setFilter(QtCore.QDir.NoDotAndDotDot | QtCore.QDir.AllDirs)
self.fileModel = QtWidgets.QFileSystemModel(self)
self.fileModel.setFilter(QtCore.QDir.NoDotAndDotDot | QtCore.QDir.Files)
self.treeview.setModel(self.dirModel)
self.listview.setModel(self.fileModel)
self.treeview.setRootIndex(self.dirModel.index(path))
self.listview.setRootIndex(self.fileModel.index(path))
self.listview.setSelectionMode(QtWidgets.QAbstractItemView.MultiSelection)
self.treeview.clicked.connect(self.on_clicked)
self.fileModel.layoutChanged.connect(self.on_layoutChanged)
@QtCore.pyqtSlot(QtCore.QModelIndex)
def on_clicked(self, index):
self.listview.clearSelection()
path = self.dirModel.fileInfo(index).absoluteFilePath()
self.listview.setRootIndex(self.fileModel.setRootPath(path))
@QtCore.pyqtSlot()
def on_layoutChanged(self):
self.listview.selectAll()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
我创建了一个 window 这样的
我该如何解决这个问题?
要select和deselect所有项目你必须分别使用selectAll()
和clearSelection()
。但是 selection 必须在更新的视图之后,为此使用 layoutChanged
信号,而且 selection 模式必须设置为 QAbstractItemView::MultiSelection
.
import sys
from PyQt5 import QtCore, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
super(Widget, self).__init__(*args, **kwargs)
hlay = QtWidgets.QHBoxLayout(self)
self.treeview = QtWidgets.QTreeView()
self.listview = QtWidgets.QListView()
hlay.addWidget(self.treeview)
hlay.addWidget(self.listview)
path = QtCore.QDir.rootPath()
self.dirModel = QtWidgets.QFileSystemModel(self)
self.dirModel.setRootPath(QtCore.QDir.rootPath())
self.dirModel.setFilter(QtCore.QDir.NoDotAndDotDot | QtCore.QDir.AllDirs)
self.fileModel = QtWidgets.QFileSystemModel(self)
self.fileModel.setFilter(QtCore.QDir.NoDotAndDotDot | QtCore.QDir.Files)
self.treeview.setModel(self.dirModel)
self.listview.setModel(self.fileModel)
self.treeview.setRootIndex(self.dirModel.index(path))
self.listview.setRootIndex(self.fileModel.index(path))
self.listview.setSelectionMode(QtWidgets.QAbstractItemView.MultiSelection)
self.treeview.clicked.connect(self.on_clicked)
self.fileModel.layoutChanged.connect(self.on_layoutChanged)
@QtCore.pyqtSlot(QtCore.QModelIndex)
def on_clicked(self, index):
self.listview.clearSelection()
path = self.dirModel.fileInfo(index).absoluteFilePath()
self.listview.setRootIndex(self.fileModel.setRootPath(path))
@QtCore.pyqtSlot()
def on_layoutChanged(self):
self.listview.selectAll()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())