I'd like to know the correct way of deleting a set of vectors of pointers in a table I create to contain checkboxes
In the defining class, I declare the variables as follow :
class tableFramer : public QWidget
{
Q_OBJECT
public :
explicit tableFramer(QWidget *parent = nullptr);
~tableFramer();
private:
QVector<QHBoxLayout*> hbLayoutPtrVector;
QVector<QWidget*> wdgPtrVector;
QVector<QCheckBox*> cbAcceptPtrVector;
QVector<QCheckBox*> cbRemovePtrVector;
}
in the implementation I create them as follow :
void tableFramer::makeForm()
{
:
ui->tableWidget->clear();
int j = 0;
ui->tableWidget->setRowCount(this->rows);
ui->tableWidget->setColumnCount(this->cols);
ui->tableWidget->verticalHeader()->setVisible(false);
ui->tableWidget->horizontalHeader()->setVisible(false);
for(int i = 0; i < this->rows; i++)
{
QHBoxLayout* hbLayout(new QHBoxLayout());
QWidget* wdg(new QWidget());
QCheckBox* applyCb(new QCheckBox(this));
QCheckBox* removeCb(new QCheckBox(this));
hbLayoutPtrVector.push_back(hbLayout);
wdgPtrVector.push_back(wdg);
cbAcceptPtrVector.push_back(applyCb);
cbRemovePtrVector.push_back(removeCb);
}
for(auto itr = hbLayoutPtrVector.begin() ; itr <= hbLayoutPtrVector.end() && j < hbLayoutPtrVector.length(); itr++ , j++ )
{
cbAcceptPtrVector.at(j)->setText("Opt 1");cbAcceptPtrVector.at(j)->setProperty("row",j);cbAcceptPtrVector.at(j)->setProperty("column",1);
cbRemovePtrVector.at(j)->setText("Opt 2");cbRemovePtrVector.at(j)->setProperty("row",j);cbRemovePtrVector.at(j)->setProperty("column",1);
(*itr)->addWidget(cbAcceptPtrVector.at(j));
(*itr)->addWidget(cbRemovePtrVector.at(j));
(*itr)->setAlignment(Qt::AlignCenter);
wdgPtrVector.at(j)->setLayout(*itr);
ui->tableWidget->setCellWidget(j,1,wdgPtrVector.at(j));
ui->tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
ui->tableWidget->verticalHeader()->setDefaultSectionSize(40);
ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
ui->tableWidget->horizontalHeader()->setDefaultSectionSize(200);
ui->tableWidget->setItem(j,0,new QTableWidgetItem(this->subfolders.at(j)));
connect(cbAcceptPtrVector.at(j),SIGNAL(toggled(bool)),this,SLOT(checkBoxAcceptToggeled(bool)));
connect(cbRemovePtrVector.at(j),SIGNAL(toggled(bool)),this,SLOT(checkBoxRejectToggeled(bool)));
}
:
}
To delete the dynamically created items, in the destructor I did the following but I don't think this is done right :
tableFramer::~tableFramer
{
:
if ( rows != 0)
{
for(int i = 0; i < rows ; ++i)
{
if(hbLayoutPtrVector[i])
delete hbLayoutPtrVector[i];
if(wdgPtrVector[i])
delete wdgPtrVector[i];
if(cbAcceptPtrVector[i])
delete cbAcceptPtrVector[i];
if(cbRemovePtrVector[i])
delete cbRemovePtrVector[i];
for(int j=0; j < 3; ++j) // there are 3 columns in total
{
delete ui->tableWidget->item(i,j);
}
}
hbLayoutPtrVector.clear();
wdgPtrVector.clear();
cbAcceptPtrVector.clear();
cbRemovePtrVector.clear();
}
rows = 0;
:
}
