Solved Why i can't replace QTableWidgetItem with another?
-
@VRonin said in Why i can't replace QTableWidgetItem with another?:
Can you show us the code you use to insert?
ui->tableWidMemory->setItem(0, 1, valOrder.at(i));
-
How do you add items to
valOrder
?What we need to answer is the complete lifetime of the
QTableWidgetItem
before it ends up inui->tableWidMemory->setItem
-
@Engelard
From the error message:QTableWidget: cannot insert an item that is already owned by another QTableWidget
A
QTableWidget
"knows" whatQTableWidgetItem
s have been inserted into it, and eachQTableWidgetItem
"knows" which (one)QTableWidget
it has been inserted into. A particularQTableWidgetItem
instance can only be stored in oneQTableWidget
, you cannot re-insert it into another.In some shape or form, your
QVector<QTableWidgetItem*>
items must have previously been inserted into anotherQTableWidget
, so you cannot also insert them into thisQTableWidget
. -
@VRonin said in Why i can't replace QTableWidgetItem with another?:
How do you add items to
valOrder
?What we need to answer is the complete lifetime of the
QTableWidgetItem
before it ends up inui->tableWidMemory->setItem
Well, add it like that:
valOrder.append(ui->tableWidMemory->item(i, 0));
valOrder declaration(which i posted above) placed in .h file, so i suppose it's lifetime = lifetime of whole program.
So as you might gues, i'm adding that items FROM tabWidget to that vector, and then from it after some time fill tabWidget with old items which i just take recently from it.
-
fill tabWidget with old items which i just take recently from it
So for a start did you remove the item before you tried to re-add it? From what you have shown of populating your
valOrder
they are existing/live items in the table widget. You cannot add the same item twice.... -
@JonB no, i dont remove existing in tabWid first. I tried, but found only removeRow function, so using it in for loop i'm removing all rows from tabWid so it empty then, but for some reason i can't fill tabWid with my valOrder after such actions. Here whole code section:
for(int i=valOrder.size(); i>0; i--) { ui->tableWidMemory->removeRow(i-1); } ui->tableWidMemory->setRowCount(valOrder.size()); for(int i=0; i<sizeof(valOrder); i++) { ui->tableWidMemory->setItem(i, 1, valOrder.at(i)); }
Would be nice if i could use some function like "replace" or "removeItemAtCell" or something like that...
-
@Engelard
I just know you won't be able to re-add the same item twice. Hence the error message (in this case, I guess the "already owned by another QTableWidget" ends up meaning "[nor] already owned by the same QTableWidget"). I don't know how you do it in your situation, but whatever (unless you can delete/remove the existing one from the table) you need to create a fresh, newQTableWidgetItem
, which does not already belong to the table, and then copy in your data or whatever from the one it's a duplicate of. Or retain theQTableWidgetItem
but change what's in it. I'm not really sure whether you're trying to add a new, duplicate one or alter an existing one. -
Ok, the other question then. How can i replace existed item in tableWdiget to some other cell? Because that's the point of all my actions.
Or even replace whole row. Like i want replace rowAt(5) to 2.
-
@Engelard said in Why i can't replace QTableWidgetItem with another?:
How can i replace existed item in tableWdiget to some other cell?
By taking a look in the documentation: http://doc.qt.io/qt-5/qtablewidget.html#takeItem
-
@Christian-Ehrlicher said in Why i can't replace QTableWidgetItem with another?:
@Engelard said in Why i can't replace QTableWidgetItem with another?:
How can i replace existed item in tableWdiget to some other cell?
By taking a look in the documentation: http://doc.qt.io/qt-5/qtablewidget.html#takeItem
But which item exactly it will take? There must be at least 3 parameters for 1.QTableWidgetItem, 2.Row to which i want it remove, 3. and column.
-
I don't understand your problem - takeItem has two parameters - row and column of the item to take and returns this item. This is exactly what you want. After this you can set the item to the new row/column with setItem().
-
@Christian-Ehrlicher Now i get it. Why they did'nt call it like "remove"... I thought that function shout "take" item from one cell and place it to the another....
-
@Engelard said in Why i can't replace QTableWidgetItem with another?:
I thought that function shout "take" item from one cell and place it to the another.
Imo the documentation is clear enough: Removes the item at row and column from the table without deleting it.
Calling sth like remove() I would expect that the item gets deleted since the QTreeWidget has the ownership.