Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to remove items from c++ model inside qml?
Forum Updated to NodeBB v4.3 + New Features

How to remove items from c++ model inside qml?

Scheduled Pinned Locked Moved QML and Qt Quick
qmlqtquickmvcmodel
38 Posts 3 Posters 17.2k Views 2 Watching
  • 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.
  • C CoderJeff

    @p3c0 said:

    Hi @CoderJeff,
    Your code looks fine. Use method1 or method3 in MySqlModel. The problem is with the view. TableView inside ScrollView seems to break. I just kept TableView in tab01 and everything worked as expected.

    What's the meaning of "kept TableView in tab01"? Can you show me the code?

    p3c0P Offline
    p3c0P Offline
    p3c0
    Moderators
    wrote on last edited by
    #22

    @CoderJeff The contents of tab01.qml file. I just TableView in it and deleted the rest. It works.

    //Tab01.qml
    
    import QtQuick 2.0
    import QtQuick.Controls 1.2
    import QtQuick.Layouts 1.1
    
    TableView{
        id: tableView
        anchors.fill: parent
        TableViewColumn{ role: "id"; title: "Id"}
        TableViewColumn{ role: "name"; title: "Name"}
        TableViewColumn{ role: "author"; title: "Author"}
        TableViewColumn{ role: "year"; title: "Year"}
        model:myCppClass.model()
    }
    

    157

    C 1 Reply Last reply
    0
    • p3c0P p3c0

      @CoderJeff The contents of tab01.qml file. I just TableView in it and deleted the rest. It works.

      //Tab01.qml
      
      import QtQuick 2.0
      import QtQuick.Controls 1.2
      import QtQuick.Layouts 1.1
      
      TableView{
          id: tableView
          anchors.fill: parent
          TableViewColumn{ role: "id"; title: "Id"}
          TableViewColumn{ role: "name"; title: "Name"}
          TableViewColumn{ role: "author"; title: "Author"}
          TableViewColumn{ role: "year"; title: "Year"}
          model:myCppClass.model()
      }
      
      C Offline
      C Offline
      CoderJeff
      wrote on last edited by
      #23

      @p3c0

      I got it.

      Yes, it works. I change ScrollView to Rectangle, and it also seems work well. But I debugged the code, and fount that the value of val0 and val1 in method1 and method3 were still not right.

      Did you check them?

      p3c0P 2 Replies Last reply
      0
      • C CoderJeff

        @p3c0

        I got it.

        Yes, it works. I change ScrollView to Rectangle, and it also seems work well. But I debugged the code, and fount that the value of val0 and val1 in method1 and method3 were still not right.

        Did you check them?

        p3c0P Offline
        p3c0P Offline
        p3c0
        Moderators
        wrote on last edited by
        #24

        @CoderJeff Hmm that's strange. It doesn't work. Tested it with my other project, works there.

        157

        C 1 Reply Last reply
        0
        • C CoderJeff

          @p3c0

          I got it.

          Yes, it works. I change ScrollView to Rectangle, and it also seems work well. But I debugged the code, and fount that the value of val0 and val1 in method1 and method3 were still not right.

          Did you check them?

          p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #25

          @CoderJeff If you replace the code in data() with the following, it will work:

          QVariant MySqlModel::data(const QModelIndex &index, int role) const
          {
              if (!index.isValid())
                  return QVariant();
          
              switch(role) {
                  case Id: {
                      return record(index.row()).value(0).toString();
                  }
                  case Name : {
                      return record(index.row()).value(1).toString();
                  }
                  case Author : {
                      return record(index.row()).value(2).toString();
                  }
                  case Year : {
                      return record(index.row()).value(3).toString();
                  }
              }
              return QSqlQueryModel::data(index, role);
          }
          
          ...
          
          setQuery(strQuery);
          var0 = record(1).value(0);
          var1 = record(0).value(1);
          qDebug() << var0 << var1;
          

          Trying to find the reason.

          157

          C 1 Reply Last reply
          0
          • p3c0P p3c0

            @CoderJeff Hmm that's strange. It doesn't work. Tested it with my other project, works there.

            C Offline
            C Offline
            CoderJeff
            wrote on last edited by
            #26

            @p3c0

            That's what I am confused with. In addition, I tested the following statements, and they all works.

            record(0).indexof(...)
            record(0).index(...)
            data(...)

            I do not understand why only record(0).value(...) can not work.

            I tried to delete a chosen row, but it could not.I do not know whether it is the above reason. I have updated my code. Can you please check them? Thank you.

            1 Reply Last reply
            0
            • p3c0P p3c0

              @CoderJeff If you replace the code in data() with the following, it will work:

              QVariant MySqlModel::data(const QModelIndex &index, int role) const
              {
                  if (!index.isValid())
                      return QVariant();
              
                  switch(role) {
                      case Id: {
                          return record(index.row()).value(0).toString();
                      }
                      case Name : {
                          return record(index.row()).value(1).toString();
                      }
                      case Author : {
                          return record(index.row()).value(2).toString();
                      }
                      case Year : {
                          return record(index.row()).value(3).toString();
                      }
                  }
                  return QSqlQueryModel::data(index, role);
              }
              
              ...
              
              setQuery(strQuery);
              var0 = record(1).value(0);
              var1 = record(0).value(1);
              qDebug() << var0 << var1;
              

              Trying to find the reason.

              C Offline
              C Offline
              CoderJeff
              wrote on last edited by
              #27

              @p3c0 said:

              @CoderJeff If you replace the code in data() with the following, it will work:

              QVariant MySqlModel::data(const QModelIndex &index, int role) const
              {
                  if (!index.isValid())
                      return QVariant();
              
                  switch(role) {
                      case Id: {
                          return record(index.row()).value(0).toString();
                      }
                      case Name : {
                          return record(index.row()).value(1).toString();
                      }
                      case Author : {
                          return record(index.row()).value(2).toString();
                      }
                      case Year : {
                          return record(index.row()).value(3).toString();
                      }
                  }
                  return QSqlQueryModel::data(index, role);
              }
              
              ...
              
              setQuery(strQuery);
              var0 = record(1).value(0);
              var1 = record(0).value(1);
              qDebug() << var0 << var1;
              

              Trying to find the reason.

              Yes, it works. The value of val0 and val1 are both right this time. But I still can not delete a row.

              p3c0P 1 Reply Last reply
              0
              • C CoderJeff

                @p3c0 said:

                @CoderJeff If you replace the code in data() with the following, it will work:

                QVariant MySqlModel::data(const QModelIndex &index, int role) const
                {
                    if (!index.isValid())
                        return QVariant();
                
                    switch(role) {
                        case Id: {
                            return record(index.row()).value(0).toString();
                        }
                        case Name : {
                            return record(index.row()).value(1).toString();
                        }
                        case Author : {
                            return record(index.row()).value(2).toString();
                        }
                        case Year : {
                            return record(index.row()).value(3).toString();
                        }
                    }
                    return QSqlQueryModel::data(index, role);
                }
                
                ...
                
                setQuery(strQuery);
                var0 = record(1).value(0);
                var1 = record(0).value(1);
                qDebug() << var0 << var1;
                

                Trying to find the reason.

                Yes, it works. The value of val0 and val1 are both right this time. But I still can not delete a row.

                p3c0P Offline
                p3c0P Offline
                p3c0
                Moderators
                wrote on last edited by
                #28

                @CoderJeff That is because QSqlquerymodel is a read-only model. See here. You will need to delete the row using another query and then update the view.

                157

                C 1 Reply Last reply
                0
                • p3c0P p3c0

                  @CoderJeff That is because QSqlquerymodel is a read-only model. See here. You will need to delete the row using another query and then update the view.

                  C Offline
                  C Offline
                  CoderJeff
                  wrote on last edited by CoderJeff
                  #29

                  @p3c0

                  I tried to use QSqlTableModel, and it also did not work.

                  Here I do not want to use another query. I just want to delete a row from the model instead of the table. In other words, only on GUI it seems to be deleted, but it is still in the table actually.

                  p3c0P 1 Reply Last reply
                  0
                  • C CoderJeff

                    @p3c0

                    I tried to use QSqlTableModel, and it also did not work.

                    Here I do not want to use another query. I just want to delete a row from the model instead of the table. In other words, only on GUI it seems to be deleted, but it is still in the table actually.

                    p3c0P Offline
                    p3c0P Offline
                    p3c0
                    Moderators
                    wrote on last edited by
                    #30

                    @CoderJeff Sorry I too donot know about this. This QTBUG-37538 seems to be similar but using ListView.
                    Test it with QTableView to see if it works ?

                    157

                    C 1 Reply Last reply
                    0
                    • p3c0P p3c0

                      @CoderJeff Sorry I too donot know about this. This QTBUG-37538 seems to be similar but using ListView.
                      Test it with QTableView to see if it works ?

                      C Offline
                      C Offline
                      CoderJeff
                      wrote on last edited by CoderJeff
                      #31

                      @p3c0

                      I used a temporary table to achieve my expected function. In addition, call setTable and select as the SqlTableModelTest did.

                      p3c0P 1 Reply Last reply
                      0
                      • C CoderJeff

                        @p3c0

                        I used a temporary table to achieve my expected function. In addition, call setTable and select as the SqlTableModelTest did.

                        p3c0P Offline
                        p3c0P Offline
                        p3c0
                        Moderators
                        wrote on last edited by p3c0
                        #32

                        @CoderJeff Ok, but then why not execute a delete query instead of creating another temporary table ? I guess removeRow for QSqlTableModel too does the same internally.

                        157

                        C 1 Reply Last reply
                        0
                        • p3c0P p3c0

                          @CoderJeff Ok, but then why not execute a delete query instead of creating another temporary table ? I guess removeRow for QSqlTableModel too does the same internally.

                          C Offline
                          C Offline
                          CoderJeff
                          wrote on last edited by
                          #33

                          @p3c0

                          Is a table not necessary when using a delete query?

                          p3c0P 1 Reply Last reply
                          0
                          • C CoderJeff

                            @p3c0

                            Is a table not necessary when using a delete query?

                            p3c0P Offline
                            p3c0P Offline
                            p3c0
                            Moderators
                            wrote on last edited by p3c0
                            #34

                            @CoderJeff table is required ofcourse but why duplicate it first. What you require can be achieved by firing another delete query and then reloading the model so that it reflects in TableView.

                            157

                            C 1 Reply Last reply
                            0
                            • p3c0P p3c0

                              @CoderJeff table is required ofcourse but why duplicate it first. What you require can be achieved by firing another delete query and then reloading the model so that it reflects in TableView.

                              C Offline
                              C Offline
                              CoderJeff
                              wrote on last edited by
                              #35

                              @p3c0

                              Actually, I was wondering what you said for an evening, and still did not understand it. Can you show me an example?

                              If not duplicate the table first, after deleting a record from the table, how can I restore it?

                              p3c0P 1 Reply Last reply
                              0
                              • C CoderJeff

                                @p3c0

                                Actually, I was wondering what you said for an evening, and still did not understand it. Can you show me an example?

                                If not duplicate the table first, after deleting a record from the table, how can I restore it?

                                p3c0P Offline
                                p3c0P Offline
                                p3c0
                                Moderators
                                wrote on last edited by
                                #36

                                @CoderJeff Since you only want to delete a row it can be done as follows:

                                //MySqlModel.cpp
                                void MySqlModel::removeMyRow(int r)
                                {
                                    QString strQuery("DELETE FROM Book where id="+QString::number(r));
                                    QSqlQuery sq;
                                    sq.prepare(strQuery);
                                    sq.exec();
                                
                                    strQuery = "SELECT * FROM Book";
                                    sq.prepare(strQuery);
                                    sq.exec();
                                    setQuery(sq);
                                }
                                

                                This will delete the row first and then reload the model with updated data.

                                157

                                C 1 Reply Last reply
                                0
                                • p3c0P p3c0

                                  @CoderJeff Since you only want to delete a row it can be done as follows:

                                  //MySqlModel.cpp
                                  void MySqlModel::removeMyRow(int r)
                                  {
                                      QString strQuery("DELETE FROM Book where id="+QString::number(r));
                                      QSqlQuery sq;
                                      sq.prepare(strQuery);
                                      sq.exec();
                                  
                                      strQuery = "SELECT * FROM Book";
                                      sq.prepare(strQuery);
                                      sq.exec();
                                      setQuery(sq);
                                  }
                                  

                                  This will delete the row first and then reload the model with updated data.

                                  C Offline
                                  C Offline
                                  CoderJeff
                                  wrote on last edited by
                                  #37

                                  @p3c0

                                  That is not what I mean.

                                  I just want to delete a row on GUI, not from the table. What you did will definitely delete a row from the table.

                                  p3c0P 1 Reply Last reply
                                  0
                                  • C CoderJeff

                                    @p3c0

                                    That is not what I mean.

                                    I just want to delete a row on GUI, not from the table. What you did will definitely delete a row from the table.

                                    p3c0P Offline
                                    p3c0P Offline
                                    p3c0
                                    Moderators
                                    wrote on last edited by p3c0
                                    #38

                                    @CoderJeff Oh. Sorry, I thought its other way. Didn't understood what you meant here.

                                    157

                                    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