0

I have a table. And an item.

How can I use only one Item to fill part of the table?

If I set item to one position and then take item, I lose text at that position. And use lots of items is not comfortable.

QTableWidgetItem *Type = new QTableWidgetItem;
if( line.contains("some"))
{
     Type->setText("some");
     ui->tableWidget->setItem(i, 0, Type);
}
else if( line.contains("shi"))
{
     Type->setText("shi");
     ui->tableWidget->setItem(i, 0, Type);
}
ui->tableWidget->takeItem(i, 0);
Leo Chapiro
  • 13,678
  • 8
  • 61
  • 92
user3360601
  • 327
  • 3
  • 17
  • You opened 3 topics for almost the same subject. I saw that you're a beginner in Qt. I advise you to read some documentation and example, understand why it is written like this before trying your own custom implementation such as using only one item to initialize an entire tableWidget. – Martin Aug 01 '14 at 14:40
  • I read documentation about QTableWidget and QtableWidgetItem. It didn't help me in this question. – user3360601 Aug 01 '14 at 14:48
  • I admit that it's not clear. In fact it is more in the model/view Qt's spirit to have one (and a unique one) item in each box of a tableWidget. Each item can have detect if you click on it for example. If you had two pointers in different boxes pointing to the same item, this property would have absolutely no sense. – Martin Aug 01 '14 at 15:04

1 Answers1

1

You can create copies of the item to insert at different cells. This can be done using clone :

QTableWidgetItem *Type1 = new QTableWidgetItem;

Type1->setText("some");
ui->tableWidget->setItem(row1, col1, Type1);

QTableWidgetItem *Type2 = Type1->clone(); // create a copy
Type2->setText("shi");
ui->tableWidget->setItem(row2, col2, Type2);
Nejat
  • 31,784
  • 12
  • 106
  • 138