Results 1 to 6 of 6

Thread: QTableView + sorting + numbers & special characters

  1. #1
    Join Date
    Dec 2008
    Location
    Lithuania
    Posts
    10
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Question QTableView + sorting + numbers & special characters

    Hi all,
    For the first - sorry for my english!!!...

    OS: WinXP
    QT: 4.5.1 (opensource)
    Compiler: MinGW

    Please help me with sorting numbers & special characters in QTableView.

    set data to QStandardItemModel
    Qt Code:
    1. itemModel = new QStandardItemModel;
    2. int row = 0;
    3. while (query->next())
    4. {
    5. item = new QStandardItem;
    6. itemModel->setItem(row, 0, item);
    7. itemModel->setData(itemModel->index(row,0), Qt::Unchecked, Qt::CheckStateRole);
    8. item = new QStandardItem(tr("%0").arg(query->record().value("ltr_cust_code").toString()));
    9. itemModel->setItem(row, 1, item);
    10. item = new QStandardItem(tr("%0").arg(query->record().value("kk").toString()));
    11. itemModel->setItem(row, 2, item);
    12. //etc........
    13. row++;
    14. }
    To copy to clipboard, switch view to plain text mode 

    set QStandardItemModel to QTableView
    Qt Code:
    1. ui->tlvXMLList2->setModel(xmlModel2);
    2. ui->tlvXMLList2->setSortingEnabled(true);
    3. ui->tlvXMLList2->sortByColumn(1, Qt::AscendingOrder);
    To copy to clipboard, switch view to plain text mode 

    First problem:
    The 2'nd column has data which is int type, and sorting is something like that [1,10,11,...,2,21,...] I need that numbers will be sorted [1,2,...,10,11,...,21,...]

    Second problem:
    In 3'rd column data is text type and has lithuanian characters (ĄČĘĖĮÅ ŲŪŽ) (if you don't see the characters set character encoding on your browser Baltic (Windows-1257)). When I'm sorting data using this column then data sorts something like that [A,B,C,D,E,F,G,...,ĄČĘĖĮ,...]. In lithuanian language sorting of text with lithuanian characters is [A,Ą,B,C,Č,D,E,Ę,Ė,F,G,...]
    How do everything what I need ???

    Thank's
    Last edited by foggy-mind; 2nd June 2009 at 12:47.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QTableView + sorting + numbers & special characters

    Quote Originally Posted by foggy-mind View Post
    First problem:
    The 2'nd column has data which is int type, and sorting is something like that [1,10,11,...,2,21,...] I need that numbers will be sorted [1,2,...,10,11,...,21,...]
    use

    Qt Code:
    1. item = new QStandardItem;
    2. item->setValue(query->record().value("ltr_cust_code").toInt());
    3. itemModel->setItem(row, 1, item);
    To copy to clipboard, switch view to plain text mode 

    if all your entries are integers...

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QTableView + sorting + numbers & special characters

    Quote Originally Posted by foggy-mind View Post
    Second problem:
    In 3'rd column data is text type and has lithuanian characters (ĄČĘĖĮÅ ŲŪŽ) (if you don't see the characters set character encoding on your browser Baltic (Windows-1257)). When I'm sorting data using this column then data sorts something like that [A,B,C,D,E,F,G,...,ĄČĘĖĮ,...]. In lithuanian language sorting of text with lithuanian characters is [A,Ą,B,C,Č,D,E,Ę,Ė,F,G,...]
    Subclass your model and alter QAbstractItemModel::sort();

    and

    Qt Code:
    1. item = new QStandardItem(tr("%0").arg(query->record().value("kk").toString()));
    To copy to clipboard, switch view to plain text mode 

    could be easily written as

    Qt Code:
    1. item = new QStandardItem(query->record().value("kk").toString());
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Dec 2008
    Location
    Lithuania
    Posts
    10
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView + sorting + numbers & special characters

    Quote Originally Posted by Lykurg View Post
    use

    Qt Code:
    1. item = new QStandardItem;
    2. item->setValue(query->record().value("ltr_cust_code").toInt());
    3. itemModel->setItem(row, 1, item);
    To copy to clipboard, switch view to plain text mode 

    if all your entries are integers...
    Yes all entries is integers... But QStandardItem don't have setValue function.....

    Quote Originally Posted by Lykurg
    Subclass your model....
    you talking about this manual??? Model Subclassing Reference
    Last edited by foggy-mind; 3rd June 2009 at 07:55.

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QTableView + sorting + numbers & special characters

    Quote Originally Posted by foggy-mind View Post
    Yes all entries is integers... But QStandardItem don't have setValue function.....
    Ah, right, confused -> setData()!

    you talking about this manual??? Model Subclassing Reference
    Yeah, why not. But is much more easier if you subclass QStandardItemModel, because then you just have to override the sort function

    Qt Code:
    1. void MyModel::sort( int column, Qt::SortOrder order)
    2. {
    3. if ("yourcolumn" == column)
    4. // do custom sorting
    5. else
    6. QStandardItemModel::sort(column, order);
    7. }
    To copy to clipboard, switch view to plain text mode 


    EDIT: Just look how QStandardItemModel implements that function and do it the "same" way.
    Last edited by Lykurg; 3rd June 2009 at 16:38.

  6. The following user says thank you to Lykurg for this useful post:

    foggy-mind (4th June 2009)

  7. #6
    Join Date
    Dec 2008
    Location
    Lithuania
    Posts
    10
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView + sorting + numbers & special characters

    Ok I'll try.... thanks

Similar Threads

  1. QTableView sorting
    By gabriels in forum Qt Programming
    Replies: 11
    Last Post: 6th October 2010, 17:13
  2. Special character's HTML entity to string
    By Hiba in forum Qt Programming
    Replies: 4
    Last Post: 3rd March 2009, 14:05
  3. Refresh QTableView after sorting
    By araglin in forum Newbie
    Replies: 4
    Last Post: 18th December 2008, 22:13
  4. special characters in xml
    By khcbabu in forum Qt Programming
    Replies: 3
    Last Post: 6th November 2008, 22:10
  5. Problem at time compilation in traslation of language
    By thomasjoy in forum Qt Programming
    Replies: 3
    Last Post: 22nd May 2007, 14:18

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.