PDA

View Full Version : read QTableWidget cells



navid
7th March 2010, 10:19
hi,

how can i read QTableWidget cells contents?

regards
n

aamer4yu
7th March 2010, 10:35
Dont you have QTableWidgetItem ?
QTableWidget::item :rolleyes:

navid
7th March 2010, 10:40
yes

if it is possible, please provide a simple code

aamer4yu
7th March 2010, 10:48
See class documentation for QTableWidgetItem.
What data exactly you mean ? and how do you fill the QTableWidget ?

navid
7th March 2010, 11:27
both text and double data

Rewo
3rd April 2010, 21:15
I've got solution - I did it today and it may help you (QT Creator 1.3.1 + Qt 4.6.2):
It's terrible thing - I needed about 2 hours :/
My example shows reading from cell with coordinates: (0,0):


QTableWidgetItem *itab = tableWidget->item(0,0);
QString itabtext = itab->text();

john_god
4th April 2010, 00:44
Be carefull with the above code, your using a pointer without alocating memory, instead do:


QString itabtext = tableWidget->item(0,0)->text();

tsp
4th April 2010, 07:05
john_god, how your example differs from Rewo's example, those seems to be same?

The issue where you should be careful is that if the cell does not have item the item()-method returns NULL which means that you should check the validity of the pointer before accessing cell's content. That said this is implementation issue, if you implement your table in a way that there is always item in every cell then this kind of check is not needed. Next is a common pattern in my own table related handling methods:



QTableWidgetItem *itab = tableWidget->item(0,0);
if (itab) {
QString itabtext = itab->text();
:
:
}

john_god
4th April 2010, 10:40
in the end, it's the same, but I'm simplifying code by eliminating itab an reading directly from item.

I agree with wath you said about cell validation, to me its all correct.

Just a side note, for writing cell content I usually do a pattern like this:


if (tableWidget->item(i,j) == 0)
{

QTableWidgetItem *newItem = new QTableWidgetItem("some text");
tableWidget->setItem(i,j, newItem);
}
else
{
tableWidgetMatrix->item(i, j)->setText("some text");
}