Results 1 to 3 of 3

Thread: QTableWidget::item to Double?

  1. #1
    Join Date
    Oct 2006
    Posts
    11
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default QTableWidget::item to Double?

    How do I convert QTableWidget::item to double?

    Qt Code:
    1. void Method::setMatrix()
    2. {
    3. for (int i = 0; i < rowCount; ++i)
    4. {
    5. for (int j = 0; j < columnCount; ++j)
    6. {
    7. a[i][j] = table->item(i, j);
    8. }
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 30th October 2006 at 08:48. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget::item to Double?

    Like this

    Qt Code:
    1. QTableWidgetItem *item = table->item(i,j);
    2. a[i][j] = item->text().toDouble();
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableWidget::item to Double?

    You can even set the data to the table widget as doubles instead of text. Then you'd have double spin boxes in editing mode (editors will be line edits if you set the data as text) and you'll avoid explicitly converting the string to a double again and again (instead you'll get directly the double contained by the variant):

    Qt Code:
    1. // set
    2. double num = ...
    3. QTableWidgetItem* item = ...
    4. item->setData(Qt::DisplayRole, num);
    5.  
    6. // get
    7. double result = item->data(Qt::DisplayRole).toDouble();
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

Similar Threads

  1. Auto text select in QTableWidgetItem
    By tstankey in forum Newbie
    Replies: 2
    Last Post: 5th October 2006, 21:40
  2. Interesting little Segfault w/r to signal/slot connection
    By Hydragyrum in forum Qt Programming
    Replies: 24
    Last Post: 12th September 2006, 20:22
  3. Replies: 1
    Last Post: 17th May 2006, 01:23
  4. Push button double click
    By curtisw in forum Qt Programming
    Replies: 3
    Last Post: 15th February 2006, 17:40
  5. Replies: 5
    Last Post: 12th January 2006, 16:40

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.