Overloaded __lt__ operator unexpectedly called
我有qtablewidget,第一列填充了可检查项,所以我需要重载这些项才能对它们进行排序:
1 2 3 4 5 6 7 8 9 10 11 | class CheckBoxItem(QtGui.QTableWidgetItem): def __init__(self, doSelect): QtGui.QTableWidgetItem.__init__(self, doSelect) self.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled) if doSelect: self.setCheckState(QtCore.Qt.Checked) else: self.setCheckState(QtCore.Qt.Unchecked) def __lt__(self, other): return self.checkState() < other.checkState() |
当我单击该列的标题时,它按预期工作:行被排序-首先有选中的行,然后没有选中。
当我不单击任何列的标题,然后使用
我有6列(0-5),如果我没有单击其中任何一列的标题进行排序,那么
那么,为什么叫
1 2 3 | def __lt__(self, other): if sortIndicatorSection==0: return self.checkState() < other.checkState() |
请帮帮我,我是个笨蛋。
我已经解决了这个问题。原来
1 | int QHeaderView::sortIndicatorSection () const |
Returns the logical index of the section that has a sort indicator.
By default this is section 0.
这可能是因为我有TableWidget而不是TableView。
所以我发现了:
1 | void QHeaderView::setSortIndicator ( int logicalIndex, Qt::SortOrder order ) |
Sets the sort indicator for the section specified by the given
logicalIndex in the direction specified by order, and removes the sort
indicator from any other section that was showing it.logicalIndex may be -1, in which case no sort indicator will be shown
and the model will return to its natural, unsorted order. Note that
not all models support this and may even crash in this case.
它适用于qtablewidget。我把这行放在表格被填充之前,这样解决了我的问题。