Results 1 to 4 of 4

Thread: QTableVIew shows number with commas but model->data(index).toString shows dots

  1. #1
    Join Date
    Jul 2008
    Location
    EU , Poland, Zabrze
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question QTableVIew shows number with commas but model->data(index).toString shows dots

    Hi
    I have a QTableView that gets its content via QSqlQueryModel as the source.
    It shows nice decimals in my locale

    ITEM NUMBER
    a 2,33
    b 7,88
    c 12,33


    but when I access
    table->model()->index(i,j).data(Qt::EditRole).toString()
    or
    table->model()->index(i,j).data(Qt:isplayRole).toString()

    I get

    ITEM NUMBER
    a 2.33
    b 7.88
    c 12.33

    any suggestions?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTableVIew shows number with commas but model->data(index).toString shows dots

    Use QLocale to do locale-aware conversion of numbers to strings.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jul 2008
    Location
    EU , Poland, Zabrze
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTableVIew shows number with commas but model->data(index).toString shows dots

    Good hint but when I use QLocale i will get QString , setting QStandardItem with qstring will make the table unsortable

    so instead of getting the double values sorted like this :

    1
    4
    11
    32

    I will get QString sorted like this :

    1
    11
    32
    4


    Is there a way to use double keeping proper formating?

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTableVIew shows number with commas but model->data(index).toString shows dots

    You can use QStandardItemModel::setSortRole() and maybe a custom role (Qt::UserRole + 1) and QStandardItem::setData() with your QStandardItem instances to set the numerical values you want to sort on, and then let the model know about it.

    The UI will continue to use whatever string has been installed using setText() (which is returned using the Qt::DisplayRole), but the items themselves will be sorted using your custom role.
    Last edited by d_stranz; 18th February 2022 at 21:31.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. QLineEdit shows always the end, how shows start ?
    By Alundra in forum Qt Programming
    Replies: 8
    Last Post: 19th June 2015, 13:09
  2. tableView shows part of model
    By Omid123 in forum Qt Programming
    Replies: 2
    Last Post: 13th February 2015, 16:46
  3. tableView shows hierarchical model
    By Omid123 in forum Newbie
    Replies: 1
    Last Post: 4th February 2015, 00:37
  4. Replies: 0
    Last Post: 29th April 2011, 10:02
  5. Change QTableView's index number displayed
    By rex in forum Qt Programming
    Replies: 0
    Last Post: 6th April 2011, 16:30

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.