Results 1 to 7 of 7

Thread: Trying to set numbers in a table but not working.

  1. #1
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default Trying to set numbers in a table but not working.

    Hi!

    I'm trying to show numbers in a QTableWidget, but my following code is showing nothing! There is no compile error. What's going on?
    Qt Code:
    1. for(int i = 0; i < LINES; i++)
    2. {
    3. for(int j = 0; j < COLS; j++)
    4. {
    5. QTableWidgetItem val( QString::number( 123 ) );//This number is only for test.
    6. ui->tableWidget->setItem(i, j, &val);
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Trying to set numbers in a table but not working.

    Create the items on heap and not on the stack.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    robgeek (4th April 2015)

  4. #3
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default Re: Trying to set numbers in a table but not working.

    Wut?

    I tried this but i get an error message:
    Qt Code:
    1. QTableWidgetItem val( QString::number( 1 ) );
    2.  
    3. for(int i = 0; i < LINES; i++)
    4. {
    5. for(int j = 0; j < COLS; j++)
    6. {
    7. val.setText( QString::number( 123 ) );
    8. ui->historicLM->setItem(i, j, &val);
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    QTableWidget: cannot insert an item that is already owned by another QTableWidget

  5. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Trying to set numbers in a table but not working.


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

    robgeek (4th April 2015)

  7. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Trying to set numbers in a table but not working.

    Quote Originally Posted by robgeek View Post
    Wut?
    As wysota wrote: allocate the items on the heap, not on the stack.

    Your "val" instance is stack allocated. The fact that you have to use the address operator to satisfy the signature of setItem() should have been quite a strong hint already.

    Cheers,
    _

  8. The following user says thank you to anda_skoa for this useful post:

    robgeek (4th April 2015)

  9. #6
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default Re: Trying to set numbers in a table but not working.

    But i used "new" and still not working!
    Bellow, the entire code.
    Qt Code:
    1. Test::Test(QWidget *parent) : QDialog( parent ), ui(new Ui::Test)
    2. {
    3. ui->setupUi( this );
    4.  
    5. ui->tableWidget->setColumnCount( 21 );
    6. QHeaderView *header = ui->tableWidget->horizontalHeader( );
    7. header->resizeSections(QHeaderView::Stretch);
    8.  
    9. QStringList colsnames("#");
    10. for(int i = 0; i < COLS; i++)
    11. colsnames << QString::number( (i + 1) );
    12.  
    13. ui->tableWidget->setHorizontalHeaderLabels( colsnames );
    14.  
    15. for(int i = 0; i < LINES; i++)
    16. {
    17. for(int j = 0; j < COLS; j++)
    18. {
    19. QTableWidgetItem *val = new QTableWidgetItem( QString::number( 123 ) );
    20. ui->tableWidget->setItem(i, j, val);
    21. }
    22. }
    23.  
    24. }
    To copy to clipboard, switch view to plain text mode 

  10. #7
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default Re: Trying to set numbers in a table but not working.

    Guys! Problem fixed. All i did was add insertRow method.
    Qt Code:
    1. for(int i = 0; i < LINES; i++)
    2. {
    3. ui->tableWidget->insertRow( i );
    4. for(int j = 0; j < COLS; j++)
    5. {
    6. QTableWidgetItem *val = new QTableWidgetItem( QString::number( 123 ) );
    7. ui->tableWidget->setItem(i, j, val);
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    Now everything is working fine.

    Thank you for your help!

Similar Threads

  1. Multiple table delegates or a single table delegate?
    By karankumar1609 in forum Qt Programming
    Replies: 2
    Last Post: 22nd August 2014, 21:28
  2. custom table model is not working
    By aurora in forum Qt Programming
    Replies: 3
    Last Post: 8th May 2012, 20:02
  3. Replies: 1
    Last Post: 8th June 2011, 14:13
  4. how to SORT table of numbers with QT
    By tommy in forum Qt Programming
    Replies: 4
    Last Post: 29th May 2009, 09:34
  5. Working out table's current row from pushbutton click
    By shooogun in forum Qt Programming
    Replies: 1
    Last Post: 16th March 2008, 23: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.