Results 1 to 8 of 8

Thread: QTableWidget - how do I initialize items?

  1. #1
    Join Date
    Dec 2010
    Location
    Israel
    Posts
    90
    Thanks
    59
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Question QTableWidget - how do I initialize items?

    This is a Newbie question so I'm in the right place to ask it.

    I have a form with a QTableWidget in it, called tblMyTable.

    I wrote this code to be executed when clicking on a push button:

    Qt Code:
    1. ui.tblMyTable->setRowCount(10);
    2. ui.tblMyTable->setColumnCount(3);
    3. QTableWidgetItem itm(QString("blablabla"));
    4.  
    5. itm.setText("blabla");
    6. ui.tblMyTable->setItem(1, 1, &itm);
    To copy to clipboard, switch view to plain text mode 

    When running, my code creates the 10x3 table but there is no data in any of the cells.

    Before I tried entering data directly using:
    Qt Code:
    1. ui.tblMyTable->item(1, 1)->setText("boo");
    To copy to clipboard, switch view to plain text mode 
    but this resulted in a read access violation error.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget - how do I initialize items?

    You are allocating your item on the stack, and once the slot exits, the item is destroyed, leaving the the pointer in the table pointing to outer space.
    Qt Code:
    1. ui.tblMyTable->setRowCount(10);
    2. ui.tblMyTable->setColumnCount(3);
    3. //QTableWidgetItem itm(QString("blablabla"));
    4.  
    5. itm.setText("blabla");
    6. ui.tblMyTable->setItem(1, 1, new QTiableItem(QString("blablabla")));
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

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

    frankiefrank (11th January 2011)

  4. #3
    Join Date
    Dec 2010
    Location
    Israel
    Posts
    90
    Thanks
    59
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget - how do I initialize items?

    Thanks for the reply, high_flyer, but in your code, is it possible you meant this:

    Quote Originally Posted by high_flyer View Post
    Qt Code:
    1. ui.tblMyTable->setRowCount(10);
    2. ui.tblMyTable->setColumnCount(3);
    3. //QTableWidgetItem itm(QString("blablabla"));
    4.  
    5. //itm.setText("blabla"); // comment this line as well
    6. // Then allocate a new object like this:
    7. ui.tblMyTable->setItem(1, 1, new QTableWidgetItem(QString("blablabla")));
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget - how do I initialize items?

    yes, sorry, I forgot to comment setText() out.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

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

    frankiefrank (11th January 2011)

  7. #5
    Join Date
    Dec 2010
    Location
    Israel
    Posts
    90
    Thanks
    59
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget - how do I initialize items?

    Thanks, that makes sense, I didn't think about the scope.

  8. #6
    Join Date
    Dec 2010
    Location
    Israel
    Posts
    90
    Thanks
    59
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: QTableWidget - how do I initialize items?

    Sorry, still having trouble with this, and getting Read Access error, I'm thinking it's scope related but I don't really understand how I'm supposed to be doing it.

    I have a table widget and I'm trying to initialize an item inside it:
    Qt Code:
    1. void TableTry::on_btnDoWork_clicked()
    2. {
    3.  
    4. int foundIdx = getOrCreateMeasurementRowIdx(ui.txtMeasurementName->text());
    5.  
    6. // Here is the code executed inside getOrCreateMeasurementRowIdx:
    7. int insertAt = ui.tblMyTable->rowCount();
    8. ui.tblMyTable->insertRow(insertAt);
    9. ui.tblMyTable->setItem(insertAt, 0, new QTableWidgetItem(measurementName)); // Parameter is the lineEdit.text()
    10.  
    11. // Back in on_btnDoWork_clicked():
    12. ui.tblMyTable->item(foundIdx,0)->setText("boooooooo"); // THIS causes error: Access violation reading location 0x00000000
    13. }
    To copy to clipboard, switch view to plain text mode 

    Is it possible I'm not supposed to be editing items I've already added? Should I always create a new item and then use setItem?

    Why can't I access the item I dynamically created?

  9. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget - how do I initialize items?

    Probably the index is wrong.
    I don't know what 'foundIndx' values is.
    Just for testing you can try using 'insertInx', and at any rate you should add
    Qt Code:
    1. ...
    2. Q_ASSERT(iIndex < ui.tblMyTable.rowCount());
    3. ui.tblMyTable->item(iIndex,0)->setText("boooooooo");
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  10. #8
    Join Date
    Dec 2010
    Location
    Israel
    Posts
    90
    Thanks
    59
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget - how do I initialize items?

    OK, I see that I should have just created new items when I wanted to replace existing items, and then use setItem. That worked for me.

    Thanks for the help.

Similar Threads

  1. QTableWidget->items
    By calireno in forum Newbie
    Replies: 2
    Last Post: 8th October 2009, 18:16
  2. Transfer items from a QTableWidget to another
    By grub87 in forum Qt Programming
    Replies: 1
    Last Post: 17th July 2009, 16:46
  3. Removing items from QtableWidget
    By algajard in forum Qt Programming
    Replies: 1
    Last Post: 17th March 2009, 09:31
  4. Replies: 1
    Last Post: 14th May 2008, 19:35
  5. adding items in qtablewidget
    By Djony in forum Qt Programming
    Replies: 17
    Last Post: 24th November 2006, 10:03

Tags for this Thread

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.