在QTableWidget中按QLineEdit排序

时间:2011-08-05 15:00:35

标签: qt qt4

我有QTableWidget,其中包含QLineEdit和普通表格单元格项的组合。我想知道按QLineEdit列中的值对表格中的数据进行排序的最佳方法。

这是一个例子。

| customer code |    header    |    message    |
------------------------------------------------
|   QLineEdit   | normal cell  |  normal cell  |
|   QLineEdit   | normal cell  |  normal cell  |
|   QLineEdit   | normal cell  |  normal cell  |
|   QLineEdit   | normal cell  |  normal cell  |
|   QLineEdit   | normal cell  |  normal cell  |

我想对客户代码列进行排序,但由于它是QLineEdit(使用setCellWidget()设置),因此表格不会按其值排序。

我知道我可以把每个值都取出来,把它们放在一个临时的物体或行中,然后将行放在一边,但这样会非常难看和缓慢。有更好的方法吗?

1 个答案:

答案 0 :(得分:1)

有几种方法可以解决这个问题,这里有几个想法:

  • 对包含行编辑的单元格使用QTableWidgetItem :: setText()。如果您将单元格上设置的文本与行编辑中的内容同步,则应保留排序行为。
  • 子类QTableWidgetItem并重载<运算符,此时您可以抓取行编辑并手动比较它们的值。