create object by new in on_pushButton_clicked() so where can i delete that object
-
void MainWindow::on_pushButton_clicked()
{
Model *model = new Model;
ui->tableView->setModel(model);
}where can i delete model, please help
@wutjakra said in create object by new in on_pushButton_clicked() so where can i delete that object:
where can i delete model, please help
Which model and why?
When a class has a setter function then there is mostly also a getter function - see the documentation.
-
@wutjakra said in create object by new in on_pushButton_clicked() so where can i delete that object:
where can i delete model, please help
Which model and why?
When a class has a setter function then there is mostly also a getter function - see the documentation.
@Christian-Ehrlicher said in create object by new in on_pushButton_clicked() so where can i delete that object:
When a class has a setter function then there is mostly also a getter function
class Model : public QAbstractTableModel
{
Q_OBJECT
public:
Model(QObject *parent = nullptr);
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
};i am copy from Model/View Tutorial. so i guess when use new i must delete it somewhere.
-
@Christian-Ehrlicher said in create object by new in on_pushButton_clicked() so where can i delete that object:
When a class has a setter function then there is mostly also a getter function
class Model : public QAbstractTableModel
{
Q_OBJECT
public:
Model(QObject *parent = nullptr);
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
};i am copy from Model/View Tutorial. so i guess when use new i must delete it somewhere.
@wutjakra
Yes, you should delete it. Before setting (calling again) theui->tableView->setModel(model)to a new one. Plus in theMainWindowdestructor to clean up for the last one, which won't have been replaced.It is "unusual" to destroy and create anew a model each time a button is clicked, but I guess you know what you are doing. It also "unusual" to store a (pointer to) a model only in a local variable in a function. You can only access that model via
ui->tableView(). Often people create a member variable to hold the current model. -
@wutjakra
Yes, you should delete it. Before setting (calling again) theui->tableView->setModel(model)to a new one. Plus in theMainWindowdestructor to clean up for the last one, which won't have been replaced.It is "unusual" to destroy and create anew a model each time a button is clicked, but I guess you know what you are doing. It also "unusual" to store a (pointer to) a model only in a local variable in a function. You can only access that model via
ui->tableView(). Often people create a member variable to hold the current model.