Issue
I have a UI with a QTableView
, which in turn uses the QStandardItemModel
for its data. Because some of my data is numeric, I subclassed the QStandardItem
to implement my own comparison. Per the docs, I only need to override the "<"
operator (the __lt__(...)
function). However, when I click the column header in the table view, the sort doesn't happen correctly. My model 'filler' is pretty simple:
self.model.removeRows(0, self.model.rowCount())
for i in range(0, len(self.mainFlatList)):
self.model.setItem(i, 0, QStandardItem(self.mainFlatList[str(i)]["ID"]))
self.model.setItem(i, 1, QStandardItem(self.mainFlatList[str(i)]["type"]))
self.model.setItem(i, 2, QStandardItem(self.mainFlatList[str(i)]["string1"]))
self.model.setItem(i, 3, QStandardItem(self.mainFlatList[str(i)]["string2"]))
self.model.setItem(i, 4, QStandardItem(self.mainFlatList[str(i)]["timeva1"]))
self.model.setItem(i, 5, QStandardItem(self.mainFlatList[str(i)]["timeval2"]))
currentOPTimePercent = (float(self.mainFlatList[str(i)]["timeval1"])/self.totalTime) * 100
self.mainFlatList[str(i)]["timePercent"] = currentOPTimePercent
self.model.setItem(i, 6, MQStandardItem(str(self.mainFlatList[str(i)]["timePercent"])))
Where MQStandardItem
is simply:
class MQStandardItem(QStandardItem):
def __init__(self, value):
print "NumericalStandardItem", value
QStandardItem.__init__(self, value)
def __lt__(self, obj):
print "Sorting!"
try:
return float(self.text()) < float(obj.text())
except:
pass
# Fallback to use standard __lt__
return self.text() < obj.text()
Any ideas on why this could be failing? I should also note that the print statement in the __lt__(...)
function doesn't even get printed, which leads me to wonder if I'm overriding things correctly. If it helps, here's how I construct the UI:
self.model = QStandardItemModel(numRows,numColumns)
self.model.setHorizontalHeaderItem(0, QStandardItem("ID"))
self.model.setHorizontalHeaderItem(1, QStandardItem("Type"))
self.model.setHorizontalHeaderItem(2, QStandardItem("string1"))
self.model.setHorizontalHeaderItem(3, QStandardItem("string2"))
self.model.setHorizontalHeaderItem(4, QStandardItem("timeval1"))
self.model.setHorizontalHeaderItem(5, QStandardItem("timeval2"))
self.model.setHorizontalHeaderItem(6, QStandardItem("timePercent"))
self.tableView = QTableView()
self.tableView.setModel(self.model)
self.tableView.setSortingEnabled(True)
self.tableView.setSelectionMode(QAbstractItemView.SingleSelection)
self.tableView.setSelectionBehavior(QAbstractItemView.SelectRows)
self.tableView.selectionModel().currentChanged.connect(self.foo)
Solution
All righty, it looks like this is a bug with Qt. There's an old post asking about this very issue:
http://www.qtcentre.org/threads/46985-any-solution-to-QtableView-sorting
and in looking through the source for the QTableView class, it seems that the connection between the view and model is still not made, at least from a look at:
and
The workaround is to use QTreeWidget, and still roll your own QTreeWidgetItem class for sorting. This works just fine.
Answered By - easythrees
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.