Results 1 to 7 of 7

Thread: [How to ?] Writing QTableWidget cells.

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

    Default [How to ?] Writing QTableWidget cells.

    Hi, I've done reading from QTableWidget cells:

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

    But i tried to make writing to QTableWidget cells:

    I tried many ways, for example:

    _____

    Code:

    Qt Code:
    1. itab->setText("X");
    2. tableWidget->setItem(0, 0, itab);
    To copy to clipboard, switch view to plain text mode 


    Result (during running the program (compilation OK)):



    _____

    Code:

    Qt Code:
    1. itab->setText("X");
    2. tableWidget->setItem(0, 0, itab->text());
    To copy to clipboard, switch view to plain text mode 


    Result (during compilation):



    Of course it's logical, because setItem needs QTableWidgetItem as third parameter, unfortunately not QString .

    Can someone help me how to write "X" into (0,0) cell ?

  2. #2
    Join Date
    Jan 2009
    Location
    Midlands UK
    Posts
    62
    Thanks
    6
    Thanked 9 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: [How to ?] Writing QTableWidget cells.

    Hi,

    QTableWidgetItem *twi ;
    twi= tableWidget->item(row, col) ;
    twi->setText(QString) ;

    SteveH

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

    Default Re: [How to ?] Writing QTableWidget cells.

    Thanks for reply.

    I did that:

    Code:

    Qt Code:
    1. itab = tableWidget->item(0,0);
    2. itab->setText("X");
    To copy to clipboard, switch view to plain text mode 


    Result (during running the program (compilation OK)):




    Program works correctly when I delete "itab->setText("X");", but without any result, ofc.
    Program works correctly too when I delete "itab = tableWidget->item(0,0);", so this 2 lines collide.

    EDIT:

    I tried this:

    Code:

    Qt Code:
    1. itab->text() = "X";
    2. tableWidget->setItem(0, 0, itab);
    To copy to clipboard, switch view to plain text mode 

    And this:

    Code:

    Qt Code:
    1. itab->text() = "X";
    2. tableWidget->item(0,0);
    To copy to clipboard, switch view to plain text mode 

    But the result is:





    EDIT XXX:

    It's very very funny, look:

    Qt Code:
    1. itab->text() = "X";
    2. QString cyce = itab->text();
    3. label -> setText(cyce);
    To copy to clipboard, switch view to plain text mode 

    OR

    Qt Code:
    1. itab -> setText("X");
    2. QString cyce = itab->text();
    3. label -> setText(cyce);
    To copy to clipboard, switch view to plain text mode 

    And the result is of course:





    Dunno. It looks like setText and itab->text() = "X"; are wrong here.
    Last edited by Rewo; 3rd April 2010 at 22:51.

  4. #4
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: [How to ?] Writing QTableWidget cells.

    You have to alocate memory to your pointer

    Qt Code:
    1. itab = new QTableWidgetItem; // add this line
    2. itab->setText("X");
    3. tableWidget->setItem(0, 0, itab);
    To copy to clipboard, switch view to plain text mode 

    It was crashing because you're setting a value to an unalolocate pointer.
    Last edited by john_god; 3rd April 2010 at 23:58.
    __________________________________________________
    My projects: calculator MathGraphica ; SuperEpicMegaHero game ; GooglePlay ; bitbucket ; github
    Like my projects ? Buy me a kofi

  5. The following user says thank you to john_god for this useful post:

    soulless (4th April 2010)

  6. #5
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: [How to ?] Writing QTableWidget cells.

    I noticed john_god's post right after I pressed "Submit Reply"

    Try this:
    Qt Code:
    1. QTableWidgetItem *itab= new QTablewidgetItem;
    2. itab->setText("X");
    3. tableWidget->setItem(0, 0, itab);
    To copy to clipboard, switch view to plain text mode 
    Last edited by norobro; 4th April 2010 at 01:35. Reason: copied wrong code

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

    Default Re: [How to ?] Writing QTableWidget cells.

    Thanks, it helped. But I have one more problem.

    I tried to apply that:

    Qt Code:
    1. itab -> setText("X");
    2. for(int a=0;a<irows;a++)
    3. for(int b=0;b<icols;b++) {
    4. tableWidget->setItem(a, b, itab);
    5. }
    To copy to clipboard, switch view to plain text mode 

    Filling "X" works only for (0,0) cell, while running program, there is statement in compiling console:

    "QTableWidget: cannot insert an item that is already owned by another QTableWidget"

    If I have 4x3 table, this statement appears 4x3-1=11 times.

    I tried to make an "itab" array, but it failed.

    I think that itab needs some release from cell, but dunno how to do that.

    ________

    EDIT 2 minutes later:

    I did that:


    Qt Code:
    1. for(int a=0;a<irows;a++)
    2. for(int b=0;b<icols;b++) {
    3. itab -> setText("X");
    4. tableWidget->setItem(a, b, itab);
    5. }
    To copy to clipboard, switch view to plain text mode 

    And it works for me .

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

    Default Re: [How to ?] Writing QTableWidget cells.

    Please let me make a sugestion, if you plan to fill the table more than once, use the following code to avoid duplicating items

    Qt Code:
    1. for(int a=0;a<irows;a++)
    2. for(int b=0;b<icols;b++){
    3.  
    4. if (tableWidget->itab(a,b) == 0)
    5. {
    6. itab->setText("X");
    7. tableWidget->setItem(a,b, itab);
    8. }
    9. else
    10. {
    11. tableWidget->item(a, b)->setText("X");
    12. }
    13.  
    14. }
    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. read QTableWidget cells
    By navid in forum Newbie
    Replies: 8
    Last Post: 4th April 2010, 10:40
  2. Merging cells in QTableWidget
    By lyucs in forum Newbie
    Replies: 1
    Last Post: 22nd January 2010, 19:15
  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.