PDA

View Full Version : QTableWidget - how do I initialize items?



frankiefrank
10th January 2011, 19:02
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:



ui.tblMyTable->setRowCount(10);
ui.tblMyTable->setColumnCount(3);
QTableWidgetItem itm(QString("blablabla"));

itm.setText("blabla");
ui.tblMyTable->setItem(1, 1, &itm);


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:


ui.tblMyTable->item(1, 1)->setText("boo");

but this resulted in a read access violation error.

high_flyer
10th January 2011, 21:00
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.


ui.tblMyTable->setRowCount(10);
ui.tblMyTable->setColumnCount(3);
//QTableWidgetItem itm(QString("blablabla"));

itm.setText("blabla");
ui.tblMyTable->setItem(1, 1, new QTiableItem(QString("blablabla")));

frankiefrank
11th January 2011, 09:36
Thanks for the reply, high_flyer, but in your code, is it possible you meant this:





ui.tblMyTable->setRowCount(10);
ui.tblMyTable->setColumnCount(3);
//QTableWidgetItem itm(QString("blablabla"));

//itm.setText("blabla"); // comment this line as well
// Then allocate a new object like this:
ui.tblMyTable->setItem(1, 1, new QTableWidgetItem(QString("blablabla")));

high_flyer
11th January 2011, 09:41
yes, sorry, I forgot to comment setText() out.

frankiefrank
11th January 2011, 10:06
Thanks, that makes sense, I didn't think about the scope.

frankiefrank
11th January 2011, 16:55
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:


void TableTry::on_btnDoWork_clicked()
{

int foundIdx = getOrCreateMeasurementRowIdx(ui.txtMeasurementName->text());

// Here is the code executed inside getOrCreateMeasurementRowIdx:
int insertAt = ui.tblMyTable->rowCount();
ui.tblMyTable->insertRow(insertAt);
ui.tblMyTable->setItem(insertAt, 0, new QTableWidgetItem(measurementName)); // Parameter is the lineEdit.text()

// Back in on_btnDoWork_clicked():
ui.tblMyTable->item(foundIdx,0)->setText("boooooooo"); // THIS causes error: Access violation reading location 0x00000000
}


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?

high_flyer
11th January 2011, 17:17
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


...
Q_ASSERT(iIndex < ui.tblMyTable.rowCount());
ui.tblMyTable->item(iIndex,0)->setText("boooooooo");

frankiefrank
11th January 2011, 18:57
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.