1

私はpyQtを使用しています。QTreeView / StandardItemModelで子アイテムの並べ替えを無効にするにはどうすればよいですか?

4

2 に答える 2

4

QSortFilterProxyModelを使用して、そのlessThanメソッドを再実装できます。

または、 QStandardItemのサブクラスを作成し、そのless演算子を再実装します。

後者のアプローチを示す簡単な例を次に示します。

from random import sample
from PyQt4 import QtGui, QtCore

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.view = QtGui.QTreeView(self)
        self.view.setHeaderHidden(True)
        self.model = QtGui.QStandardItemModel(self.view)
        self.view.setModel(self.model)
        parent = self.model.invisibleRootItem()
        keys = range(65, 91)
        for key in sample(keys, 10):
            item = StandardItem('Item %s' % chr(key), False)
            parent.appendRow(item)
            for key in sample(keys, 10):
                item.appendRow(StandardItem('Child %s' % chr(key)))
        self.view.sortByColumn(0, QtCore.Qt.AscendingOrder)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.view)

class StandardItem(QtGui.QStandardItem):
    def __init__(self, text, sortable=True):
        QtGui.QStandardItem.__init__(self, text)
        self.sortable = sortable

    def __lt__(self, other):
        if getattr(self.parent(), 'sortable', True):
            return QtGui.QStandardItem.__lt__(self, other)
        return False

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())
于 2012-01-27T19:23:56.497 に答える
1

setSortingEnabled(bool)QTreeViewインスタンスを呼び出します。これがc++に対応するドキュメントであり、この関数のpyqtapidocuへのリンクです

于 2012-01-27T06:21:29.060 に答える