Results 1 to 4 of 4

Thread: QTableWidget and Decimal places

  1. #1
    Join Date
    Sep 2007
    Posts
    36
    Thanks
    5
    Thanked 3 Times in 1 Post

    Default QTableWidget and Decimal places

    Qt Code:
    1. //Getting the two QTableWidgetItem below
    2. QTableWidgetItem *item1 = this->item(r, 1);
    3. QTableWidgetItem *item2 = this->item(r, 2);
    4.  
    5. //Copying the value of ietm1 to item2
    6. item2->setText(item1->text());
    To copy to clipboard, switch view to plain text mode 

    If the value of item 1 is "100.10" then it is copied as "100.1", how to make it copy as it is - "100.10"

    Also is it possible to filter the input to the qtablewidget. I want only decimal input to be accpeted.

    Thanks !
    Last edited by jpn; 17th August 2008 at 20:34.

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableWidget and Decimal places

    I have the same problem and I wrote the following workaround:
    Qt Code:
    1. QVariant value = item1->data(Qt::DisplayRole);
    2. // This is trick, because QItemDelegatePrivate::valueToText incorrect convert numbers with floating point
    3. if (value.type() == QVariant::Double)
    4. item2->setData(Qt::DisplayRole, value.toString());
    To copy to clipboard, switch view to plain text mode 

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

    pshah.mumbai (17th August 2008)

  4. #3
    Join Date
    Sep 2007
    Posts
    36
    Thanks
    5
    Thanked 3 Times in 1 Post

    Default Re: QTableWidget and Decimal places

    thanks !

    a few questions though

    Q1. Is the text() same as Data(Qt:isplayRole) ?

  5. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableWidget and Decimal places

    yup
    Qt Code:
    1. inline void QTableWidgetItem::setText(const QString &atext)
    2. { setData(Qt::DisplayRole, atext); }
    To copy to clipboard, switch view to plain text mode 

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.