
Originally Posted by
jpn
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():
item->setSomething(...);
QTableWidgetItem* item = ui.tableWidget->item(row, col);
item->setSomething(...);
To copy to clipboard, switch view to plain text mode
Hmm... not work 
{
Q_OBJECT
...
...
...
private:
...
...
...
class HSM : public QDialog
{
Q_OBJECT
...
...
...
private:
QTableWidgetItem* itemArchive;
QTableWidgetItem* itemRestore;
QTableWidgetItem* itemDelete;
...
...
...
To copy to clipboard, switch view to plain text mode
HSM
::HSM(QWidget *parent, Qt
::WFlags flags
){
...
...
...
...
...
...
HSM::HSM(QWidget *parent, Qt::WFlags flags)
: QDialog(parent, flags)
{
...
...
...
itemArchive = new QTableWidgetItem(tr("Archive"));
itemRestore = new QTableWidgetItem(tr("Restore"));
itemDelete = new QTableWidgetItem(tr("Delete"));
...
...
...
To copy to clipboard, switch view to plain text mode
void HSM::setTableData()
{
sTskInfoGUI sTaskGUI;
db_work db;
sTaskGUI = db.GetTaskToGUI();
db.CloseDB();
for (int row = 0 ; row < sTaskGUI.nCmd.size() ;++row)
{
if (sTaskGUI.nCmd.at(row) == 1)
{
itemArchive = ui.tableWidget->item(row,0);
itemArchive
->setIcon
(QIcon(QPixmap(":/HSM/Resources/archive.png")));
itemArchive
->setBackground
(QColor::fromRgb(200,
255,
104,
100));
//ui.tableWidget->setItem(row,0,itemArchive);
}
if (sTaskGUI.nCmd.at(row) == 2)
{
itemRestore = ui.tableWidget->item(row,0);
itemRestore
->setIcon
(QIcon(QPixmap(":/HSM/Resources/restore.png")));
itemRestore
->setBackground
(QColor::fromRgb(115,
150,
255,
100));
//ui.tableWidget->setItem(row,0,itemRestore);
}
if (sTaskGUI.nCmd.at(row) == 3)
{
itemDelete = ui.tableWidget->item(row,0);
itemDelete
->setIcon
(QIcon(QPixmap(":/HSM/Resources/del.png")));
itemDelete
->setBackground
(QColor::fromRgb(255,
100,
120,
100));
//ui.tableWidget->setItem(row,0,itemDelete);
}
...
...
...
void HSM::setTableData()
{
sTskInfoGUI sTaskGUI;
db_work db;
sTaskGUI = db.GetTaskToGUI();
db.CloseDB();
for (int row = 0 ; row < sTaskGUI.nCmd.size() ;++row)
{
if (sTaskGUI.nCmd.at(row) == 1)
{
itemArchive = ui.tableWidget->item(row,0);
itemArchive->setIcon(QIcon(QPixmap(":/HSM/Resources/archive.png")));
itemArchive->setBackground(QColor::fromRgb(200,255,104,100));
//ui.tableWidget->setItem(row,0,itemArchive);
}
if (sTaskGUI.nCmd.at(row) == 2)
{
itemRestore = ui.tableWidget->item(row,0);
itemRestore->setIcon(QIcon(QPixmap(":/HSM/Resources/restore.png")));
itemRestore->setBackground(QColor::fromRgb(115,150,255,100));
//ui.tableWidget->setItem(row,0,itemRestore);
}
if (sTaskGUI.nCmd.at(row) == 3)
{
itemDelete = ui.tableWidget->item(row,0);
itemDelete->setIcon(QIcon(QPixmap(":/HSM/Resources/del.png")));
itemDelete->setBackground(QColor::fromRgb(255,100,120,100));
//ui.tableWidget->setItem(row,0,itemDelete);
}
...
...
...
To copy to clipboard, switch view to plain text mode
Exception in code :
d:\qt4\src\gui\itemviews\qtablewidget.cpp
{ setData(Qt::DecorationRole, aicon); }
inline void QTableWidgetItem::setIcon(const QIcon &aicon)
{ setData(Qt::DecorationRole, aicon); }
To copy to clipboard, switch view to plain text mode
Bookmarks