PDA

View Full Version : reading and writing data from a QTableWidget



zorro68
29th January 2007, 18:59
I need to read data from a file and write its values into a QTableWidget. I programm this:

TBLiradopts->insertRow(row);
TBLiradopts->item(row,col)->setText(qv);

where qv is the datavalue (Qstring), row and col change (in a loop).

But I obtained an error

Excepción no controlada en 0x6541ab55 (QtGuid4.dll) en QTDAM.exe: 0xC0000005: Infracción de acceso al leer la ubicación 0x00000000.

And point to this:

inline void QTableWidgetItem::setText(const QString &atext)
{ setData(Qt::DisplayRole, atext); }

What's the problem?
Thanks

jacek
29th January 2007, 19:27
Probably you try to dereference a null pointer.

Most likely here:
TBLiradopts->item(row,col)->setText(qv);
Check whether item() doesn't return 0.

zorro68
29th January 2007, 20:02
I have change the code and works but I dont know why:

TBLiradopts->insertRow(row);
QTableWidgetItem *newItem=new QTableWidgetItem(row*col);
newItem->setText(qv);
TBLiradopts->setItem(row,col,newItem);

do you know why, because row and col have the same values (in my last post)?

Thanks

jacek
29th January 2007, 20:30
do you know why, because row and col have the same values (in my last post)?
The docs say:
QTableWidgetItem * QTableWidget::item ( int row, int column ) const
Returns the item for the given row and column if one has been set; otherwise returns 0.
See also setItem().
Have you set a QTableWidgetItem for given row and column before trying to access it through item()?

zorro68
29th January 2007, 20:51
No. I think that is the problem.

Thanks for your quick answer.