How to make copy of QTableWidgetItem?
-
It appears to be a real problem, i can't make a copy of item from QTableWidget1 and place it in QTableWidget2.
Program runs, but in output messages:QTableWidget: cannot insert an item that is already owned by another QTableWidget
Tried to make separated, new instance of item and assign it with first, but not working...
QTableWidgetItem *temp = ui->tableWid->item(0, 0);
-
@Engelard
AQTableWidgetItem
has a "pointer" in it so that it knows whichQTableWidget
it belongs to. A given item can only belong to one table/parent.So you cannot just "copy" a
QTableWidgetItem
and stuff it into another table. You must create a new, blank one, set fields one-by-one from the one you want to copy from, and then add it to the new owner. -
@Engelard
something likeQTableWidgetItem::clone()
? :)
Which is literallynew QTableWidgetItem( *ui->tableWid->item(0,0) )
and also doesn't assign a QTableWidget (view):
QTableWidgetItem::QTableWidgetItem(const QTableWidgetItem &other) : rtti(Type), values(other.values), view(0), d(new QTableWidgetItemPrivate(this)), itemFlags(other.itemFlags) { }
-
@raven-worx oh, i forgot to mention that i'm must using QVector of QTabWidgetItems, so how should i treat that? Next code not working:
copyItem.append(new QTableWidgetItem(ui->tableWid->item(0, 1));
-
@Engelard said in How to make copy of QTableWidgetItem?:
copyItem.append(new QTableWidgetItem(ui->tableWid->item(0, 1));
this is not the code i've posted. Note the
*
And as i said, for the sake of simplicity just callclone()
copyItem.append( ui->tableWid->item(0,1)->clone() );
-
@raven-worx said in How to make copy of QTableWidgetItem?:
something like
QTableWidgetItem::clone()
? :)
Which is literallynew QTableWidget( *ui->tableWid->item(0,0) )
To avoid confusion would you care to correct your second line in your earlier post above from
QTableWidget
toQTableWidgetItem
? -
@JonB
done