Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

setIcon crash when i'm using tableView



  • 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.

    image.png

    But, when I do this
    a.png

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


  • @eyllanesc Thank you! Indeed, the (2,2) case was empty...


Log in to reply