Hello,
I 'm still a beginner with C++ and I'm having trouble with pointers.
I've got a class named Product, inside this class I have a function which builds a table using the QT library and it looks like this
Now, what sould I do with the pointers name, price, ..., pb_details? Should I delete them to release the memory?
I was trying to delete them at the end of the for loop, but if I run the code, the program crashes.
Outside the for loop, those pointers result to not be defined.
Do I need to delete those pointers? How?
I 'm still a beginner with C++ and I'm having trouble with pointers.
I've got a class named Product, inside this class I have a function which builds a table using the QT library and it looks like this
C++:
void Product::slotProducts()
{
QList<QMap<QString, QString>> products = this->getList();
QStringList headers{ "Name", "Price" , ..., "Actions"};
for (int i = 0; i < products.size(); i++) {
// Items to put inside the table's cells
QTableWidgetItem* name = new QTableWidgetItem(products[i]["name"]);
QTableWidgetItem* price = new QTableWidgetItem(products[i]["price"]);
// other fields ...
// Push button at the end of each table's row to show the product's details
QPushButton* pb_details = new QPushButton(this->ui.tableView);
// Asssign the items to the proper cell
this->ui.tableWidget->setItem(i, 0, name);
this->ui.tableWidget->setItem(i, 1, price);
// other fields ...
// Add the push button
this->ui.tableWidget->setCellWidget(i, 11, pb_details);
// Connect the push button click to the slotProductDetails() slot
this->connect(pb_details, &QPushButton::clicked, this, &Product::slotProductDetails);
}
}
Now, what sould I do with the pointers name, price, ..., pb_details? Should I delete them to release the memory?
I was trying to delete them at the end of the for loop, but if I run the code, the program crashes.
Outside the for loop, those pointers result to not be defined.
Do I need to delete those pointers? How?