2011-08-05 49 views
0

我有一個QTableWidget,它具有QLineEdit和普通表單元格項的組合。我想知道通過QLineEdit列中的值對錶中的數據進行排序的最佳方法。按QLineEdit在QTableWidget中排序

這裏是一個例子。

| 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設置())表不會排序它的價值。

我知道我可以拿出每個值,將它們放置在一個臨時對象或行中,然後對這些行進行混洗,但這會非常難看和緩慢。請告訴我,在Qt中有更好的方法來做到這一點!

回答

1

有幾種方法可以解決這個,這裏有一對夫婦的想法:)在你的細胞中有行他們編輯

  • 使用QTableWidgetItem ::的setText(。如果您將單元格上設置的文本與您的行編輯內容保持同步,則應該保留排序行爲。
  • 子類QTableWidgetItem和過載<操作符,此時您可以抓取行編輯並手動比較它們的值。
+0

第三種方法是轉換爲使用models/views/delegates,這使得排序非常簡單。 –