setIcon crash when i'm using tableView
Solved
General and Desktop
-
Hi!
I created a Tableview 4x4 with QT design.
I have an icon in the (0,0) cell. I want to put this same icon into the (2,2) cell only when I click on the (0,0) cell. When I run this code, it crashes. I don't know why.But, when I do this
it works. I don't know why I have to create another item... Is there a way to use setIcon() without it crashes?
Thank you ! -
@fres cellClicked is emitted when the cell has been clicked even when there is no associated QTableWidgetItem so in your case the item probably does not exist, the solution is to check and if it does not exist then you must create it:
QTableWidgetItem *item = ui->tableWidget->item(a, b); if(!item){ item = new QTableWidgetItem; ui->tableWidget->setItem(a, b, item); } item->setIcon(ui->tableWidget->item(0, 0)->icon());