Search code examples
qtqtablewidgetqtablewidgetitem

one item for 5 positions


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);

Solution

  • 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);