Results 1 to 2 of 2

Thread: How does the QTableWidget sort a column?

  1. #1
    Join Date
    Oct 2009
    Posts
    18
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How does the QTableWidget sort a column?

    As I've written in another thread, I have subclassed

    Qt Code:
    1. class QInt64Item : public QTableWidgetItem
    2. {
    3. public:
    4. QInt64Item(qint64 data=0);
    5. QVariant data(int role) const;
    6. bool operator< ( const QInt64Item& other ) const;
    7. protected:
    8. qint64 m_data;
    9. };
    To copy to clipboard, switch view to plain text mode 

    and sorting the data (clicking on the header of the table) works (=sorted as expected) as long as
    Qt Code:
    1. QInt64Item::data(Qt::DisplayRole)
    To copy to clipboard, switch view to plain text mode 
    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
    Qt Code:
    1. bool QInt64Item::operator< ( const QInt64Item& other ) const
    2. {
    3. return m_data < other.m_data;
    4. }
    To copy to clipboard, switch view to plain text mode 

    can't possible be used for this.
    So how does this acctually work?
    Last edited by codemonkey; 4th October 2009 at 10:33. Reason: missing [code] tags

  2. #2
    Join Date
    Oct 2009
    Posts
    18
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default [SOLVED ]Re: How does the QTableWidget sort a column?

    I solved it.
    Here is the code.

    Qt Code:
    1. bool QInt64Item::operator< ( const QTableWidgetItem& other ) const
    2. {
    3. qDebug()<<"jamför";
    4. return m_data < ((QInt64Item&)other).m_data;
    5. }
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. QVariant QInt64Item::data(int role) const
    2. {
    3. QLocale defaultLocale;
    4. if (role==Qt::DisplayRole)
    5. return defaultLocale.toString(m_data);
    To copy to clipboard, switch view to plain text mode 
    Nice!

Similar Threads

  1. QTableWidget stretch a column other than the last one
    By roleroz in forum Qt Programming
    Replies: 6
    Last Post: 4th February 2015, 06:35
  2. Replies: 0
    Last Post: 9th February 2009, 20:57
  3. Replies: 2
    Last Post: 20th August 2008, 15:55
  4. QTableWidget column width and resizing
    By shooogun in forum Qt Programming
    Replies: 2
    Last Post: 16th March 2008, 22:31
  5. QTableWidget column and row sizes
    By Arthur in forum Qt Programming
    Replies: 4
    Last Post: 27th January 2006, 11:03

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.