PDA

View Full Version : [How to ?] Writing QTableWidget cells.



Rewo
3rd April 2010, 21:30
Hi, I've done reading from QTableWidget cells:


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

But i tried to make writing to QTableWidget cells:

I tried many ways, for example:

_____

Code:


QTableWidgetItem *itab;
itab->setText("X");
tableWidget->setItem(0, 0, itab);


Result (during running the program (compilation OK)):

http://i39.tinypic.com/21njfxv.jpg

_____

Code:


QTableWidgetItem *itab;
itab->setText("X");
tableWidget->setItem(0, 0, itab->text());


Result (during compilation):

http://i43.tinypic.com/2jbtrgi.jpg

Of course it's logical, because setItem needs QTableWidgetItem as third parameter, unfortunately not QString :(.

Can someone help me how to write "X" into (0,0) cell ?

SteveH
3rd April 2010, 22:05
Hi,

QTableWidgetItem *twi ;
twi= tableWidget->item(row, col) ;
twi->setText(QString) ;

SteveH

Rewo
3rd April 2010, 22:09
Thanks for reply.

I did that:

Code:


QTableWidgetItem *itab;
itab = tableWidget->item(0,0);
itab->setText("X");


Result (during running the program (compilation OK)):

http://i39.tinypic.com/21njfxv.jpg


Program works correctly when I delete "itab->setText("X");", but without any result, ofc.
Program works correctly too when I delete "itab = tableWidget->item(0,0);", so this 2 lines collide.

EDIT:

I tried this:

Code:


QTableWidgetItem *itab;
itab->text() = "X";
tableWidget->setItem(0, 0, itab);

And this:

Code:


QTableWidgetItem *itab;
itab->text() = "X";
tableWidget->item(0,0);

But the result is:


http://i39.tinypic.com/21njfxv.jpg


EDIT XXX:

It's very very funny, look:


QTableWidgetItem *itab;
itab->text() = "X";
QString cyce = itab->text();
label -> setText(cyce);

OR


QTableWidgetItem *itab;
itab -> setText("X");
QString cyce = itab->text();
label -> setText(cyce);

And the result is of course:


http://i39.tinypic.com/21njfxv.jpg


Dunno. It looks like setText and itab->text() = "X"; are wrong here.

john_god
3rd April 2010, 23:53
You have to alocate memory to your pointer


QTableWidgetItem *itab;
itab = new QTableWidgetItem; // add this line
itab->setText("X");
tableWidget->setItem(0, 0, itab);

It was crashing because you're setting a value to an unalolocate pointer.

norobro
4th April 2010, 00:03
I noticed john_god's post right after I pressed "Submit Reply"

Try this:

QTableWidgetItem *itab= new QTablewidgetItem;
itab->setText("X");
tableWidget->setItem(0, 0, itab);

Rewo
4th April 2010, 10:09
Thanks, it helped. But I have one more problem.

I tried to apply that:


QTableWidgetItem *itab = new QTableWidgetItem;
itab -> setText("X");
for(int a=0;a<irows;a++)
for(int b=0;b<icols;b++) {
tableWidget->setItem(a, b, itab);
}

Filling "X" works only for (0,0) cell, while running program, there is statement in compiling console:

"QTableWidget: cannot insert an item that is already owned by another QTableWidget"

If I have 4x3 table, this statement appears 4x3-1=11 times.

I tried to make an "itab" array, but it failed.

I think that itab needs some release from cell, but dunno how to do that.

________

EDIT 2 minutes later:

I did that:



for(int a=0;a<irows;a++)
for(int b=0;b<icols;b++) {
QTableWidgetItem *itab = new QTableWidgetItem;
itab -> setText("X");
tableWidget->setItem(a, b, itab);
}

And it works for me :).

john_god
4th April 2010, 11:00
Please let me make a sugestion, if you plan to fill the table more than once, use the following code to avoid duplicating items



for(int a=0;a<irows;a++)
for(int b=0;b<icols;b++){

if (tableWidget->itab(a,b) == 0)
{
QTableWidgetItem *itab = new QTableWidgetItem;
itab->setText("X");
tableWidget->setItem(a,b, itab);
}
else
{
tableWidget->item(a, b)->setText("X");
}

}