Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. create object by new in on_pushButton_clicked() so where can i delete that object

create object by new in on_pushButton_clicked() so where can i delete that object

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 375 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wutjakra
    wrote on last edited by
    #1

    void MainWindow::on_pushButton_clicked()
    {
    Model *model = new Model;
    ui->tableView->setModel(model);
    }

    where can i delete model, please help

    Christian EhrlicherC 1 Reply Last reply
    0
    • W wutjakra

      void MainWindow::on_pushButton_clicked()
      {
      Model *model = new Model;
      ui->tableView->setModel(model);
      }

      where can i delete model, please help

      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      W 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        @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.

        W Offline
        W Offline
        wutjakra
        wrote on last edited by
        #3

        @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.

        JonBJ 1 Reply Last reply
        0
        • W wutjakra

          @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.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @wutjakra
          Yes, you should delete it. Before setting (calling again) the ui->tableView->setModel(model) to a new one. Plus in the MainWindow destructor 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.

          W 1 Reply Last reply
          0
          • JonBJ JonB

            @wutjakra
            Yes, you should delete it. Before setting (calling again) the ui->tableView->setModel(model) to a new one. Plus in the MainWindow destructor 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.

            W Offline
            W Offline
            wutjakra
            wrote on last edited by
            #5

            @JonB thank you.i could i restart my code.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved