Results 1 to 5 of 5

Thread: how to SORT table of numbers with QT

  1. #1
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default how to SORT table of numbers with QT

    I have a table (rows and columns with integers) and I'd like to sort the rows lexicographically (the rows that start with smaller numbers should be first).

    Does Qt have a function for it? I was thinking that maybe I could turn rows of the table into strings and then put all the strings into a QStringList. Then I could sort the strings using string comparison and then unravel the string contents and put them back in the original table. Does this sound doable or does Qt offer a better method for my problem?

    Thanks!

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    509
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to SORT table of numbers with QT

    Hi, does QTableWidget::sortItems() do what you want?

    Ginsengelf

  3. The following user says thank you to Ginsengelf for this useful post:

    tommy (29th May 2009)

  4. #3
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to SORT table of numbers with QT

    maybe qSort() with custom LessThan function can do that.
    As I understand you right, you want to sort by column 1, but if there is same value in different rows, then you compare values in column 2 and so on? What is the data structure in which you keep your data?
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  5. The following user says thank you to faldzip for this useful post:

    tommy (29th May 2009)

  6. #4
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to SORT table of numbers with QT

    Thanks

    Qt Code:
    1. void QTableWidget::sortItems ( int column, Qt::SortOrder order = Qt::AscendingOrder )
    To copy to clipboard, switch view to plain text mode 

    The above function indeed sorts items. I was a bit unclear about whether it sorts rows based on columns or vice versa? It can only sort the rows one column at time which means that the table has the be sorted once for each column (very many times). Isn't this a bit slow? If rows could be converted into strings then rows could be sorted as it they were simple words (although they consis of numbers) using string coparison and the result could be a bit faster? Or am I wrong?

    Thanks for any ideas you might have!

  7. #5
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to SORT table of numbers with QT

    Hi Faldzip,

    You are exactly right about what I need to do. My data structure is currently a simple 2D array. I thought about qsort(), too, but it gets pretty complicated because the qsort() is not designed to drag along other data that it is not directly sorting.

    Thanks

Similar Threads

  1. Postgresql QSqlRelationalTableModel empty table
    By RolandHughes in forum Qt Programming
    Replies: 0
    Last Post: 12th November 2008, 17:18
  2. Replies: 3
    Last Post: 5th October 2008, 23:41
  3. sort mysql table
    By eleanor in forum Qt Programming
    Replies: 2
    Last Post: 10th October 2007, 16:42
  4. displaying any table on a qdatatable
    By Philip_Anselmo in forum Newbie
    Replies: 4
    Last Post: 9th May 2006, 22:12
  5. creating table plugin
    By mgurbuz in forum Qt Programming
    Replies: 3
    Last Post: 28th April 2006, 13:50

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.