Quote Originally Posted by jpn View Post
No, that shouldn't leak memory since QTableWidget deletes the old item if you set a new item into cell. But you definitely don't want to re-create all the items every time in setTableData(). Create the items once in constructor or so, and just set the data in setTableData():
Qt Code:
  1. QTableWidgetItem* item = ui.tableWidget->item(row, col);
  2. item->setSomething(...);
To copy to clipboard, switch view to plain text mode 
Hmm... not work

Qt Code:
  1. class HSM : public QDialog
  2. {
  3. Q_OBJECT
  4. ...
  5. ...
  6. ...
  7. private:
  8. QTableWidgetItem* itemArchive;
  9. QTableWidgetItem* itemRestore;
  10. QTableWidgetItem* itemDelete;
  11. ...
  12. ...
  13. ...
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. HSM::HSM(QWidget *parent, Qt::WFlags flags)
  2. : QDialog(parent, flags)
  3. {
  4.  
  5. ...
  6. ...
  7. ...
  8. itemArchive = new QTableWidgetItem(tr("Archive"));
  9. itemRestore = new QTableWidgetItem(tr("Restore"));
  10. itemDelete = new QTableWidgetItem(tr("Delete"));
  11. ...
  12. ...
  13. ...
To copy to clipboard, switch view to plain text mode 


Qt Code:
  1. void HSM::setTableData()
  2. {
  3. sTskInfoGUI sTaskGUI;
  4. db_work db;
  5.  
  6. sTaskGUI = db.GetTaskToGUI();
  7. db.CloseDB();
  8.  
  9. for (int row = 0 ; row < sTaskGUI.nCmd.size() ;++row)
  10. {
  11.  
  12. if (sTaskGUI.nCmd.at(row) == 1)
  13. {
  14. itemArchive = ui.tableWidget->item(row,0);
  15. itemArchive->setIcon(QIcon(QPixmap(":/HSM/Resources/archive.png")));
  16. itemArchive->setBackground(QColor::fromRgb(200,255,104,100));
  17. //ui.tableWidget->setItem(row,0,itemArchive);
  18.  
  19. }
  20. if (sTaskGUI.nCmd.at(row) == 2)
  21. {
  22. itemRestore = ui.tableWidget->item(row,0);
  23. itemRestore->setIcon(QIcon(QPixmap(":/HSM/Resources/restore.png")));
  24. itemRestore->setBackground(QColor::fromRgb(115,150,255,100));
  25. //ui.tableWidget->setItem(row,0,itemRestore);
  26. }
  27. if (sTaskGUI.nCmd.at(row) == 3)
  28. {
  29. itemDelete = ui.tableWidget->item(row,0);
  30. itemDelete->setIcon(QIcon(QPixmap(":/HSM/Resources/del.png")));
  31. itemDelete->setBackground(QColor::fromRgb(255,100,120,100));
  32. //ui.tableWidget->setItem(row,0,itemDelete);
  33. }
  34. ...
  35. ...
  36. ...
To copy to clipboard, switch view to plain text mode 



Exception in code :
d:\qt4\src\gui\itemviews\qtablewidget.cpp
Qt Code:
  1. inline void QTableWidgetItem::setIcon(const QIcon &aicon)
  2. { setData(Qt::DecorationRole, aicon); }
To copy to clipboard, switch view to plain text mode