重载的运算符意外调用

2024-09-30 06:24:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个QTableWidget,第一列填充了可检查项,所以我需要重载这些项才能对它们进行排序:

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()

当我单击该列的标题时,它的工作和预期一样:行被排序-首先有选中的行,然后没有被选中。在

问题当我不点击任何列的标题然后使用tableWidget.setSortingEnabled(1)时,如果我有~15行随机混合一些行,而不是对它们进行排序,那么{}会被调用100+次。 在调用setSortingEnabled(1)之前,我已经检查/取消选中了一些项目,但我不希望__lt__对它们进行比较,因为我没有单击该列的标题对它们进行排序。在

我有6列(0-5),如果我没有单击其中任何一列的标题进行排序,tableWidget.horizontalHeader().sortIndicatorSection()返回{}-这是表之外的列,这意味着表没有排序。在

那么,为什么要调用__lt__覆盖时是否遗漏了某些内容? 我认为这可以解决问题(只在单击该列的标题时比较项),但是如何将sortIndicatorSection传递给__lt__?在

^{pr2}$

请帮帮我,我是个笨蛋。在


Tags: ltself标题排序initdefqtother
1条回答
网友
1楼 · 发布于 2024-09-30 06:24:01

我解决了。原来^{{cd1>}返回“6”(我不知道为什么),但是排序的行为就像是0,因为它在文档中被写入:

    int QHeaderView::sortIndicatorSection () const

Returns the logical index of the section that has a sort indicator. By default this is section 0.

这可能是因为我有tableWidget而不是tableView。

所以我发现了:

^{pr2}$

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。我已经在填充表^{cd2>}之前放了这行,这解决了我的问题。

相关问题 更多 >

    热门问题