Results 1 to 5 of 5

Thread: Sorting data in QTable

  1. #1
    Join Date
    Jul 2009
    Posts
    36
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Sorting data in QTable

    I want to sort the column data in the table but the data is in string format and I want to do in Integer format.

    Qt Code:
    1. ID = 0;
    2.  
    3. tbl_item12 = new QTableWidgetItem (QTableWidgetItem (QString::number(ID)));
    4.  
    5. ui->tableWidget->setItem( CurRow,12 ,tbl_item12 );
    6. ui->tableWidget->sortByColumn(12,Qt::DescendingOrder);
    7.  
    8. but I get error on shorting Integer
    9.  
    10. tbl_item13 = new QTableWidgetItem (QTableWidgetItem (ID));
    To copy to clipboard, switch view to plain text mode 

    Can anyone help me to insert the integer value in column and sort it?

    If possible send me the snippets of the code.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Sorting data in QTable

    Why does it matter to you if the int is represented as string?
    The sorting should give same results I think....

    If you absolutely want to use integers, you will have to subclass QTableWidgetItem and use UserType.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jul 2009
    Posts
    36
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Sorting data in QTable

    If you sorting with String the result will not get like Integer.

    from 1-9 it sort well but after 9 the result like this

    3
    2
    12
    11
    10
    1

    0

    If possible can you send me the snippets of the code.It would be great help for me.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Sorting data in QTable

    [QUOTE][from 1-9 it sort well but after 9 the result like this/QUOTE]
    One way of overcoming this is by adding '0's like that:
    001
    002
    020
    100
    Then the strings will sort correct for the integers.

    The other way is as I said, to subclass QTableWidgetItem.

    [QUOTE][If possible can you send me the snippets of the code./QUOTE]
    Snippets of what code?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Sorting data in QTable

    another method would be to use a table model so you have access to the integer data, sort that, and then in your 'data' method, you would convert the integer data to string data to be displayed by the table.

Similar Threads

  1. sorting Data in QTable
    By sosanjay in forum Qt Programming
    Replies: 6
    Last Post: 24th October 2009, 01:21
  2. QTable Sorting
    By mromey in forum Qt Programming
    Replies: 1
    Last Post: 23rd April 2009, 07:28
  3. how to access QCheckTableItem data with in a QTable.
    By ashukla in forum Qt Programming
    Replies: 1
    Last Post: 15th September 2007, 11:55
  4. Displaying data in QTable
    By raghvendramisra in forum Qt Tools
    Replies: 1
    Last Post: 6th September 2007, 12:48
  5. QTable - Data Base Problem
    By esq in forum Qt Tools
    Replies: 9
    Last Post: 27th May 2007, 23:08

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
  •  
Qt is a trademark of The Qt Company.