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. Deleting row from QTableWidget and from Sqlite database
Qt 6.11 is out! See what's new in the release blog

Deleting row from QTableWidget and from Sqlite database

Scheduled Pinned Locked Moved Solved General and Desktop
71 Posts 6 Posters 52.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.
  • R Risver

    @JonB said in Deleting row from QTableWidget and from Sqlite database:

    @Risver said in Deleting row from QTableWidget and from Sqlite database:

    Thank You Christian, now i have no errors, but it is not removing.

    Start by showing us your new line, because we don't know which of the two methods you changed to using and what parameters you gave it.

    i've changed only this one:

    void MainWindow::on_remove_clicked()
    {
        if (ui->tableView->selectionModel()->hasSelection())
        {
            int addressId = ui->tableView->selectionModel()->currentIndex().row();
            querymodel->removeRows(addressId, 1);
        }
    }
    
    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by
    #33

    @Risver

    qDebug() << querymodel->rowCount();
    qDebug() << querymodel->removeRows(addressId, 1);
    qDebug() << querymodel->rowCount();
    
    R 1 Reply Last reply
    1
    • JonBJ JonB

      @Risver

      qDebug() << querymodel->rowCount();
      qDebug() << querymodel->removeRows(addressId, 1);
      qDebug() << querymodel->rowCount();
      
      R Offline
      R Offline
      Risver
      wrote on last edited by
      #34

      @JonB said in Deleting row from QTableWidget and from Sqlite database:

      @Risver

      qDebug() << querymodel->rowCount();
      qDebug() << querymodel->removeRows(addressId, 1);
      qDebug() << querymodel->rowCount();
      

      The rowCount is always 3 and the removeRows is false.

      1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #35

        Does your table has a primary key? Otherwise removing will not work.

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

        R JonBJ 2 Replies Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          Does your table has a primary key? Otherwise removing will not work.

          R Offline
          R Offline
          Risver
          wrote on last edited by
          #36

          @Christian-Ehrlicher said in Deleting row from QTableWidget and from Sqlite database:

          Does your table has a primary key? Otherwise removing will not work.

          Yes, it has.

          1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            Does your table has a primary key? Otherwise removing will not work.

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

            @Christian-Ehrlicher said in Deleting row from QTableWidget and from Sqlite database:

            Does your table has a primary key? Otherwise removing will not work.

            That's very interesting. I assume we're talking about a SQL table model here? I looked through the docs (briefly) but did not see any mention of this requirement, is it documented?

            R 1 Reply Last reply
            0
            • JonBJ JonB

              @Christian-Ehrlicher said in Deleting row from QTableWidget and from Sqlite database:

              Does your table has a primary key? Otherwise removing will not work.

              That's very interesting. I assume we're talking about a SQL table model here? I looked through the docs (briefly) but did not see any mention of this requirement, is it documented?

              R Offline
              R Offline
              Risver
              wrote on last edited by
              #38

              @JonB said in Deleting row from QTableWidget and from Sqlite database:

              @Christian-Ehrlicher said in Deleting row from QTableWidget and from Sqlite database:

              Does your table has a primary key? Otherwise removing will not work.

              That's very interesting. I assume we're talking about a SQL table model here? I looked through the docs (briefly) but did not see any mention of this requirement, is it documented?

              f7d18cb5-d2c3-44e2-ab9d-adbcc725c397-image.png
              That is how my SQL Table looks.

              JonBJ 1 Reply Last reply
              0
              • R Risver

                @JonB said in Deleting row from QTableWidget and from Sqlite database:

                @Christian-Ehrlicher said in Deleting row from QTableWidget and from Sqlite database:

                Does your table has a primary key? Otherwise removing will not work.

                That's very interesting. I assume we're talking about a SQL table model here? I looked through the docs (briefly) but did not see any mention of this requirement, is it documented?

                f7d18cb5-d2c3-44e2-ab9d-adbcc725c397-image.png
                That is how my SQL Table looks.

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

                @Risver

                qDebug() << querymodel->removeRows(addressId, 1);
                

                Please add qDebug() << addressId, we need to make sure what that is, just in case.....

                R 1 Reply Last reply
                1
                • JonBJ JonB

                  @Risver

                  qDebug() << querymodel->removeRows(addressId, 1);
                  

                  Please add qDebug() << addressId, we need to make sure what that is, just in case.....

                  R Offline
                  R Offline
                  Risver
                  wrote on last edited by Risver
                  #40

                  @JonB said in Deleting row from QTableWidget and from Sqlite database:

                  @Risver

                  qDebug() << querymodel->removeRows(addressId, 1);
                  

                  Please add qDebug() << addressId, we need to make sure what that is, just in case.....

                  It is a variable to store the selected row, for example when i select the first row, it returns 0.
                  I checked this and its working fine

                  JonBJ 1 Reply Last reply
                  0
                  • R Risver

                    @JonB said in Deleting row from QTableWidget and from Sqlite database:

                    @Risver

                    qDebug() << querymodel->removeRows(addressId, 1);
                    

                    Please add qDebug() << addressId, we need to make sure what that is, just in case.....

                    It is a variable to store the selected row, for example when i select the first row, it returns 0.
                    I checked this and its working fine

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

                    @Risver
                    OK, so independent of your selection querymodel->removeRows(0, 1); fails to remove the first row? We can then rule out anything to do with the UI or the selection.

                    R 2 Replies Last reply
                    1
                    • JonBJ JonB

                      @Risver
                      OK, so independent of your selection querymodel->removeRows(0, 1); fails to remove the first row? We can then rule out anything to do with the UI or the selection.

                      R Offline
                      R Offline
                      Risver
                      wrote on last edited by
                      #42

                      @JonB
                      Exactly.

                      JonBJ 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @Risver
                        OK, so independent of your selection querymodel->removeRows(0, 1); fails to remove the first row? We can then rule out anything to do with the UI or the selection.

                        R Offline
                        R Offline
                        Risver
                        wrote on last edited by
                        #43

                        @JonB said in Deleting row from QTableWidget and from Sqlite database:

                        @Risver
                        OK, so independent of your selection querymodel->removeRows(0, 1); fails to remove the first row? We can then rule out anything to do with the UI or the selection.

                        Do you have something specific in mind ?

                        1 Reply Last reply
                        0
                        • R Risver

                          @JonB
                          Exactly.

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

                          @Risver
                          Hang on. Does QSqlQueryModel allow any removing it all? It's a query, it won't/shouldn't do anything to the database for sure.

                          [Yep, I'm sure, QSqlQueryModel is a read only model, you can't remove rows from it.]

                          If you are wanting to alter the underlying the database table, I think you should be using QSqlTableModel instead?

                          I leave this to you, or @Christian-Ehrlicher, now, as I'm done for the week :)

                          R 1 Reply Last reply
                          2
                          • JonBJ JonB

                            @Risver
                            Hang on. Does QSqlQueryModel allow any removing it all? It's a query, it won't/shouldn't do anything to the database for sure.

                            [Yep, I'm sure, QSqlQueryModel is a read only model, you can't remove rows from it.]

                            If you are wanting to alter the underlying the database table, I think you should be using QSqlTableModel instead?

                            I leave this to you, or @Christian-Ehrlicher, now, as I'm done for the week :)

                            R Offline
                            R Offline
                            Risver
                            wrote on last edited by
                            #45

                            @JonB said in Deleting row from QTableWidget and from Sqlite database:

                            @Risver
                            Hang on. Does QSqlQueryModel allow any removing it all? It's a query, it won't/shouldn't do anything to the database for sure.

                            [Yep, I'm sure, QSqlQueryModel is a read only model, you can't remove rows from it.]

                            If you are wanting to alter the underlying the database table, I think you should be using QSqlTableModel instead?

                            I leave this to you, or @Christian-Ehrlicher, now, as I'm done for the week :)

                            Okay, thank You, I will try to use QSqlTableModel.

                            1 Reply Last reply
                            0
                            • R Offline
                              R Offline
                              Risver
                              wrote on last edited by Risver
                              #46

                              I've changed the QSqlQueryModel into QSqlTableModel and everything is working !
                              Thank you all ! :)

                              I have one more question - how can i refresh the table at the current time ?
                              8201cec4-25b6-4066-a082-31ebafd7efaf-image.png

                              Done it !

                              1 Reply Last reply
                              0
                              • R Offline
                                R Offline
                                Risver
                                wrote on last edited by
                                #47

                                I have one more question - can i get data from column and replace these with other data ?

                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #48

                                  Hi,

                                  Do you mean an entire column ?

                                  Interested in AI ? www.idiap.ch
                                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  R 1 Reply Last reply
                                  0
                                  • SGaistS SGaist

                                    Hi,

                                    Do you mean an entire column ?

                                    R Offline
                                    R Offline
                                    Risver
                                    wrote on last edited by
                                    #49

                                    @SGaist said in Deleting row from QTableWidget and from Sqlite database:

                                    Hi,

                                    Do you mean an entire column ?

                                    Hi, yes i mean all the cells that are in the column

                                    1 Reply Last reply
                                    0
                                    • SGaistS Offline
                                      SGaistS Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #50

                                      Yes, select the whole column and then use a loop to parse the selection.

                                      Interested in AI ? www.idiap.ch
                                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      R 1 Reply Last reply
                                      1
                                      • SGaistS SGaist

                                        Yes, select the whole column and then use a loop to parse the selection.

                                        R Offline
                                        R Offline
                                        Risver
                                        wrote on last edited by
                                        #51

                                        @SGaist
                                        Okay and can i replace the old data with new data ?

                                        JonBJ 1 Reply Last reply
                                        0
                                        • R Risver

                                          @SGaist
                                          Okay and can i replace the old data with new data ?

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

                                          @Risver
                                          What "new data", from where? Do you mean something like over-pasting a selected column's data with other data in the QTreeView UI? Do you mean updating all rows to change one column's data in the model and/or the SQL database? Programatically or by user interaction? Or what?

                                          R 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