Results 1 to 8 of 8

Thread: integer as a QTableWidgetItem ?

  1. #1
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default integer as a QTableWidgetItem ?

    I'd like to load QTableWidget with integers and then display selected integers. It seems that QTableWidget can accept integers as shown here:

    Qt Code:
    1. QTableWidget myTable = new QTableWidget(10, 10, this); //make a 10x10 table
    2.  
    3. QTableWidgetItem *newItem = new QTableWidgetItem(1); //convert from integer
    4. myTable->setItem(0, 0, newItem); //set object
    To copy to clipboard, switch view to plain text mode 

    But how do you visualize a selected entry from this table (entry 0,0)? The below code evidently does not work because the returned type is wrong.

    Qt Code:
    1. int a = myTable->item(0,0);
    To copy to clipboard, switch view to plain text mode 

    I'd have to do this
    Qt Code:
    1. QTableWidgetItem *a = myTable->item(0,0);
    To copy to clipboard, switch view to plain text mode 

    But how can I get my integer out of this? This won't return integer.

  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: integer as a QTableWidgetItem ?

    Qt Code:
    1. int val = -1;
    2. QTableWidgetItem *a = myTable->item(0,0);
    3. if (a)
    4. val = a->text().toInt();
    To copy to clipboard, switch view to plain text mode 

    You may also want have a look at QTableWidgetItem::data() and setData woth UserRole...

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

    tommy (29th May 2009)

  4. #3
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: integer as a QTableWidgetItem ?

    This worked great thanks!

    How do you empty the QTableWidget memory after you are done using the table and want to get rid of it?

  5. #4
    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: integer as a QTableWidgetItem ?

    QTableWidget::clear() or QTableWidget::clearContents(). Also frees the memory:
    Qt Code:
    1. void QTableModel::clearContents()
    2. {
    3. for (int i = 0; i < tableItems.count(); ++i) {
    4. if (tableItems.at(i)) {
    5. tableItems.at(i)->view = 0;
    6. delete tableItems.at(i); // <--
    7. tableItems[i] = 0;
    8. }
    9. }
    10. reset();
    11. }
    To copy to clipboard, switch view to plain text mode 

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

    tommy (29th May 2009)

  7. #5
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: integer as a QTableWidgetItem ?

    Thanks again!

    But can I just delete the QTableWidgetItem object? This should free the memory, too. How woud you do that?

  8. #6
    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: integer as a QTableWidgetItem ?

    Quote Originally Posted by tommy View Post
    But can I just delete the QTableWidgetItem object? This should free the memory, too. How woud you do that?
    If you only want to delete a single item which is allready assigned to the table use
    Qt Code:
    1. QTableWidget::takeItem ( int row, int column )
    To copy to clipboard, switch view to plain text mode 
    but be aware that you must delete the returned item yourself!

    Example:
    Qt Code:
    1. QTableWidgetItem *a = myTable->takeItem(0,0);
    2. if (a)
    3. delete a;
    To copy to clipboard, switch view to plain text mode 

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

    tommy (29th May 2009)

  10. #7
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: integer as a QTableWidgetItem ?

    Hi Lykurg,

    Unfortunately it didn't work. The below code for some reason always assigns 0 to val. Why doesn't it assign 3 to val?

    Thanks!

    Qt Code:
    1. QTableWidget myTable = new QTableWidget(5, 5, this);
    2.  
    3. myTable->setItem(3, 3, newItem);
    4.  
    5. QTableWidgetItem *a = myTable->item(3,3);
    6. int val = a->text().toInt();
    To copy to clipboard, switch view to plain text mode 

  11. #8
    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: integer as a QTableWidgetItem ?

    Quote Originally Posted by tommy View Post
    Qt Code:
    1. QTableWidgetItem *newItem = new QTableWidgetItem(1); //convert from integer
    To copy to clipboard, switch view to plain text mode 
    Quote Originally Posted by tommy View Post
    Why doesn't it assign 3 to val?
    because the above statement is wrong. You have to use:
    Qt Code:
    1. QTableWidgetItem *newItem = new QTableWidgetItem(QString::number(1));
    To copy to clipboard, switch view to plain text mode 

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

    tommy (29th May 2009)

Similar Threads

  1. QTableWidgetItem not enabled
    By Raccoon29 in forum Newbie
    Replies: 2
    Last Post: 21st March 2008, 18:39
  2. Replies: 1
    Last Post: 21st August 2007, 16:25
  3. QTableWidgetItem and formatting / input masks
    By Walter in forum Qt Programming
    Replies: 3
    Last Post: 27th July 2007, 13:39
  4. Inserting Integer Values to table.
    By kenny_isles in forum Newbie
    Replies: 2
    Last Post: 22nd February 2007, 07:17
  5. Interesting little Segfault w/r to signal/slot connection
    By Hydragyrum in forum Qt Programming
    Replies: 24
    Last Post: 12th September 2006, 19:22

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.