View Full Version : How does the QTableWidget sort a column?

4th October 2009, 11:17
As I've written in another thread, I have subclassed

class QInt64Item : public QTableWidgetItem
QInt64Item(qint64 data=0);
QVariant data(int role) const;
bool operator< ( const QInt64Item& other ) const;
qint64 m_data;

and sorting the data (clicking on the header of the table) works (=sorted as expected) as long as
QInt64Item::data(Qt::DisplayRole) returns the qint64 variable m_data, and not QString::number(m_data).

This puzzles me. What sort is acctually beeing run when clicking in the table header?

My comparison function

bool QInt64Item::operator< ( const QInt64Item& other ) const
return m_data < other.m_data;

can't possible be used for this. :confused:
So how does this acctually work?

4th October 2009, 14:21
I solved it.
Here is the code.

bool QInt64Item::operator< ( const QTableWidgetItem& other ) const
return m_data < ((QInt64Item&)other).m_data;

Notice that the parameter is of the type QTableWidgetItem&. That was my mistake. Now it works, and I can add pretty-printing

QVariant QInt64Item::data(int role) const
QLocale defaultLocale;
if (role==Qt::DisplayRole)
return defaultLocale.toString(m_data);