Results 1 to 9 of 9

Thread: read QTableWidget cells

  1. #1
    Join Date
    Oct 2009
    Posts
    90
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default read QTableWidget cells

    hi,

    how can i read QTableWidget cells contents?

    regards
    n

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: read QTableWidget cells

    Dont you have QTableWidgetItem ?
    QTableWidget::item

  3. #3
    Join Date
    Oct 2009
    Posts
    90
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: read QTableWidget cells

    yes

    if it is possible, please provide a simple code

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: read QTableWidget cells

    See class documentation for QTableWidgetItem.
    What data exactly you mean ? and how do you fill the QTableWidget ?

  5. #5
    Join Date
    Oct 2009
    Posts
    90
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: read QTableWidget cells

    both text and double data

  6. #6
    Join Date
    Apr 2010
    Posts
    19
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: read QTableWidget cells

    I've got solution - I did it today and it may help you (QT Creator 1.3.1 + Qt 4.6.2):
    It's terrible thing - I needed about 2 hours :/
    My example shows reading from cell with coordinates: (0,0):

    Qt Code:
    1. QTableWidgetItem *itab = tableWidget->item(0,0);
    2. QString itabtext = itab->text();
    To copy to clipboard, switch view to plain text mode 
    Last edited by Rewo; 3rd April 2010 at 21:32.

  7. #7
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    287
    Thanks
    23
    Thanked 31 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: read QTableWidget cells

    Be carefull with the above code, your using a pointer without alocating memory, instead do:

    Qt Code:
    1. QString itabtext = tableWidget->item(0,0)->text();
    To copy to clipboard, switch view to plain text mode 
    __________________________________________________
    My projects: calculator MathGraphica ; SuperEpicMegaHero game ; GooglePlay ; bitbucket ; github
    Like my projects ? Buy me a kofi

  8. #8
    Join Date
    Sep 2009
    Location
    Finland
    Posts
    63
    Thanks
    1
    Thanked 22 Times in 19 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: read QTableWidget cells

    john_god, how your example differs from Rewo's example, those seems to be same?

    The issue where you should be careful is that if the cell does not have item the item()-method returns NULL which means that you should check the validity of the pointer before accessing cell's content. That said this is implementation issue, if you implement your table in a way that there is always item in every cell then this kind of check is not needed. Next is a common pattern in my own table related handling methods:

    Qt Code:
    1. QTableWidgetItem *itab = tableWidget->item(0,0);
    2. if (itab) {
    3. QString itabtext = itab->text();
    4. :
    5. :
    6. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    287
    Thanks
    23
    Thanked 31 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: read QTableWidget cells

    in the end, it's the same, but I'm simplifying code by eliminating itab an reading directly from item.

    I agree with wath you said about cell validation, to me its all correct.

    Just a side note, for writing cell content I usually do a pattern like this:

    Qt Code:
    1. if (tableWidget->item(i,j) == 0)
    2. {
    3.  
    4. QTableWidgetItem *newItem = new QTableWidgetItem("some text");
    5. tableWidget->setItem(i,j, newItem);
    6. }
    7. else
    8. {
    9. tableWidgetMatrix->item(i, j)->setText("some text");
    10. }
    To copy to clipboard, switch view to plain text mode 
    __________________________________________________
    My projects: calculator MathGraphica ; SuperEpicMegaHero game ; GooglePlay ; bitbucket ; github
    Like my projects ? Buy me a kofi

Similar Threads

  1. Merging cells in QTableWidget
    By lyucs in forum Newbie
    Replies: 1
    Last Post: 22nd January 2010, 19:15
  2. Filling empty cells in QTableWidget
    By lyucs in forum Newbie
    Replies: 2
    Last Post: 20th October 2009, 17:12
  3. Focus of cells in a QTableWidget
    By SailinShoes in forum Qt Programming
    Replies: 4
    Last Post: 9th June 2008, 08:19
  4. QTableWidget, header cells margin
    By DpoHro in forum Qt Programming
    Replies: 4
    Last Post: 17th January 2008, 23:38
  5. QTableWidget with overlapping cells (span)
    By rhi in forum Qt Programming
    Replies: 2
    Last Post: 13th May 2006, 18:44

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.