Sort by QLineEdit in QTableWidget
I have a QTableWidget
that has a combination of QLineEdit
and normal table cell items. I would like to know the best way to sort the data in the table by the values in the QLineEd开发者_如何学Goit
column.
Here is an example.
| 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 |
I would like to sort on the customer code column but because it's a QLineEdit
(set with setCellWidget()
), the table won't sort by its values.
I know I could take each value out, place them in a temporary object or row and shuffle the rows around but that would be amazingly ugly and slow. Is there a better way to do this?
There are several ways to approach this, here are a couple of ideas:
- Use QTableWidgetItem::setText() on your cells that have the line edits in them. If you keep the text that's set on the cell sync'd with what's in your line edits then the sorting behavior should be preserved.
- Subclass QTableWidgetItem and overload the < operator, at which point you can grab the line edits out and compare their values manually.
精彩评论