python - PyQt4: Sorting a QTableWidget by a column containing an icon -
i have database list of items, , each item has associated "rating" field, float ranging 0-5. now, when i'm populating qtablewidget these items, want display image rating instead of float. this:
iconpath = "images/icons/ratings/"+backend.utilities.pickratingicon(col) icon = qtgui.qicon(qtgui.qpixmap(iconpath)) item = qtgui.qtablewidgetitem(icon, "") self.decktable.setitem(i, j, item)
everything works, naturally lose ability sort column. can change third line this:
item = qtgui.qtablewidgetitem(icon, col)
which allows me sort again, float next icon kind of defeats purpose. there way display icon sort value?
first of must store ratings.
item = qtgui.qtablewidgetitem(icon, "") self.decktable.itemsrating[item]=rating self.decktable.setitem(i, j, item)
and override sortitems subclassed qtablewidget , manually rearrange items:
class decktablewidget(qtgui.qtablewidget): itemsrating=dict() def sortitems(self, column, order=qtcore.qt.ascendingorder): _list=[] row in self.rowcount(): item = self.takeitem(row, column) _list.append((item, self.itemsrating[item])) _list.sort(lambda x,y: cmp(x.[1], y.[1]) if order != qtcore.qt.ascendingorder: _list.reverse() row in self.rowcount(): self.setitem(row, column, _list.pop()[0])
Comments
Post a Comment