我正在寻找QstandardItemModel自然排序方法。我正在阅读一些相关的问题并尝试我的代码。但它不起作用
我的工具通过拖放接受某些文件列表。我想通过自然排序对我的模型数据进行排序。并把它放到listview。
所以这是我的代码:
class VideolistView (QtWidgets.QListView):
def __init__(self, parent):
super(VideolistView, self).__init__(parent)
self.setAcceptDrops(True)
self.setObjectName("VideolistView")
self.setGeometry(QtCore.QRect(8, 30, 250, 301))
self.Model = QtGui.QStandardItemModel(self)
def dragEnterEvent(self, event):
if event.mimeData().hasUrls:
event.accept()
else:
event.ignore()
def dragMoveEvent(self, event):
if event.mimeData().hasUrls:
event.accept()
else:
event.ignore()
def dropEvent(self, event):
if event.mimeData().hasUrls:
event.setDropAction(QtCore.Qt.CopyAction)
event.accept()
for url in event.mimeData().urls():
dropitem = str(url.toLocalFile())
Fname = os.path.split(dropitem)
if not self.Model.findItems(Fname[1]):
listitem = QtGui.QStandardItem(Fname[1])
self.Model.appendRow(listitem)
self.Model.sort(0)
self.setModel(self.Model)
else:
event.ignore()
我的一些尝试宣布。
import re
def _human_key(key):
parts = re.split('(\d*\.\d+|\d+)', key)
return tuple((e.swapcase() if i % 2 == 0 else float(e))
for i, e in enumerate(parts))
......
listitems=[]
for url in event.mimeData().urls():
dropitem = str(url.toLocalFile())
Fname = os.path.split(dropitem)
listitem = QtGui.QStandardItem(Fname[1])
listitems.appent(listitem)
listitems.sort(key=_human_key)
Pycharm调试错误listitems.sort(key = _human_key)我尝试重新实现QstandardItemModel类
import re
def _human_key(key):
parts = re.split('(\d*\.\d+|\d+)', key)
return tuple((e.swapcase() if i % 2 == 0 else float(e))
for i, e in enumerate(parts))
......
Class NaturalSortModel (QtGui.QstandardItemModel)
def __lt__(self, other):
column = self.treeWidget().sortColumn()
k1 = self.text(column)
k2 = other.text(column)
return _human_key(k1) < _human_key(k2)
.......
Class VideolistView (QtWidgets.QListView)
def __init__(self, parent):
self.Model = self.NaturalSortModel(self)
........
self.Model.appendRow(listitem)
self.Model.sort(0)
self.setModel(self.Model)
........
以上两个代码错误。我如何使用QstandardItemModel自然排序?
link:Python - Human sort of numbers with alpha numeric, but in pyQt and a __lt__ operator
使用@eyllanesc答案,使用QSortFilterProxyModel的第一个解决方案效果很好。 它按自然顺序对数据进行排序。
但@eyllanesc第二个建议(Qstandarditem继承)对我不起作用。它按字母顺序排序就像原始来源一样。 @eyllanesc非常感谢!!!
答案 0 :(得分:0)
如果您想要建立特定的订单标准,您必须通过继承自QSortFilterProxyModel
的模型来实现,并实现方法lessThan
,如下所示:
class NaturalSortFilterProxyModel(QtCore.QSortFilterProxyModel):
@staticmethod
def _human_key(key):
parts = re.split('(\d*\.\d+|\d+)', key)
return tuple((e.swapcase() if i % 2 == 0 else float(e)) for i, e in enumerate(parts))
def lessThan(self, left, right):
leftData = self.sourceModel().data(left)
rightData = self.sourceModel().data(right)
return self._human_key(leftData) < self._human_key(rightData)
class VideolistView (QtWidgets.QListView):
def __init__(self, parent=None):
super(VideolistView, self).__init__(parent)
self.setAcceptDrops(True)
self.setObjectName("VideolistView")
self.setGeometry(QtCore.QRect(8, 30, 250, 301))
model = QtGui.QStandardItemModel(self)
proxyModel = NaturalSortFilterProxyModel()
proxyModel.setSourceModel(model)
proxyModel.sort(0, QtCore.Qt.AscendingOrder)
self.setModel(proxyModel)
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.accept()
else:
event.ignore()
def dragMoveEvent(self, event):
if event.mimeData().hasUrls():
event.accept()
else:
event.ignore()
def dropEvent(self, event):
if event.mimeData().hasUrls():
event.setDropAction(QtCore.Qt.CopyAction)
event.accept()
for url in event.mimeData().urls():
dropitem = str(url.toLocalFile())
Fname = os.path.split(dropitem)
if not self.model().sourceModel().findItems(Fname[1]):
listitem = QtGui.QStandardItem(Fname[1])
self.model().sourceModel().appendRow(listitem)
else:
event.ignore()
正如@ekhumuro所说,链接中显示的解决方案是指创建一个继承自QStandardItem
的类,如下所示
class NaturalStandardItem(QtGui.QStandardItem):
@staticmethod
def _human_key(key):
parts = re.split('(\d*\.\d+|\d+)', key)
return tuple((e.swapcase() if i % 2 == 0 else float(e)) for i, e in enumerate(parts))
def __lt__(self, other):
return self._human_key(self.text()) < self._human_key(other.text())
class VideolistView (QtWidgets.QListView):
def __init__(self, parent=None):
super(VideolistView, self).__init__(parent)
self.setAcceptDrops(True)
self.setObjectName("VideolistView")
self.setGeometry(QtCore.QRect(8, 30, 250, 301))
model = QtGui.QStandardItemModel(self)
self.setModel(model)
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.accept()
else:
event.ignore()
def dragMoveEvent(self, event):
if event.mimeData().hasUrls():
event.accept()
else:
event.ignore()
def dropEvent(self, event):
if event.mimeData().hasUrls():
event.setDropAction(QtCore.Qt.CopyAction)
event.accept()
for url in event.mimeData().urls():
dropitem = str(url.toLocalFile())
Fname = os.path.split(dropitem)
if not self.model().findItems(Fname[1]):
listitem = NaturalStandardItem(Fname[1])
self.model().appendRow(listitem)
self.model().sort(0)
else:
event.ignore()