I have a dialog with a QTableWidget and 2 line edit's.
The user input on the 2 line edits
determine the number of rows and columns of QTableWidget.
Also have a random function called from a popup menu
that fills the table with random numbers like this:
void matrix_dlg::matrixRandom()
{
int i;
int l;
int rows = m_ui->tableWidget_matrix1->rowCount();
int columns = m_ui->tableWidget_matrix1->columnCount();
for(i=0;i<rows;i++)
{
for(l=0;l<columns;l++)
{
str.sprintf("%d",qrand()/100);
if (m_ui->tableWidget_matrix1->item(i,l) == 0)
{
m_ui->tableWidget_matrix1->setItem(i,l, newItem);
}
else
{
m_ui->tableWidget_matrix1->item(i, l)->setText(str);
}
}
}
}
void matrix_dlg::matrixRandom()
{
QString str;
int i;
int l;
int rows = m_ui->tableWidget_matrix1->rowCount();
int columns = m_ui->tableWidget_matrix1->columnCount();
for(i=0;i<rows;i++)
{
for(l=0;l<columns;l++)
{
str.sprintf("%d",qrand()/100);
if (m_ui->tableWidget_matrix1->item(i,l) == 0)
{
QTableWidgetItem *newItem = new QTableWidgetItem(str);
m_ui->tableWidget_matrix1->setItem(i,l, newItem);
}
else
{
m_ui->tableWidget_matrix1->item(i, l)->setText(str);
}
}
}
}
To copy to clipboard, switch view to plain text mode
I put a if else to avoid creating new items to the same cell,
I think this is a correct aproach to avoid memory leak (please correct me if I'm wrong)
In the destructor, created by Qtcreator, I have
matrix_dlg::~matrix_dlg()
{
delete m_ui;
}
matrix_dlg::~matrix_dlg()
{
delete m_ui;
}
To copy to clipboard, switch view to plain text mode
This will be enought to delete the items and free memory or I should add proper code ?
Bookmarks