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. [SOLVED] remove row from QTableView with QSQLTableModel
QtWS25 Last Chance

[SOLVED] remove row from QTableView with QSQLTableModel

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 5.5k 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.
  • R Offline
    R Offline
    rfringuello89
    wrote on last edited by
    #1

    Hi! I have a QTableView filled using a mySQL database through QSQLTableModel.
    I can edit the values and soon they are updated on the database as expected.
    The problem comes when I try to delete one record, I tried different ways as I found some examples on the Internet. The "best" snippet I found is the following but even this doesn't work.
    What am I doing wrong?
    The record that I want to delete is the currently selected (only one row can be selected at once).

    @ //allBooksModel: QSqlTableModel
    //ui->allBooksTable: QTableView
    QModelIndexList list = ui->allBooksTable->selectionModel()->selectedIndexes();
    QModelIndex index = list.at(0);

    allBooksModel->removeRow(index.row(), index);@
    

    Thanks ;)

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

      Hi,

      IIRC, you should just call removeRow(index.row())

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

      1 Reply Last reply
      0
      • R Offline
        R Offline
        rfringuello89
        wrote on last edited by
        #3

        Oh wow, the constructor asks also for the QModelndex so i fought a lot for nothing!

        Now I can remove the line, also from the database.. but.. it removes the wrong line!!

        I hope to find out what's the problem!!
        Thanks again

        1 Reply Last reply
        0
        • R Offline
          R Offline
          rfringuello89
          wrote on last edited by
          #4

          Kind of strange problem.
          A wrong row is removed because the elements are sorted just before to delete the row! I'm using QSortFilterProxyModel for sorting and filtering...

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

            You are getting the index from your proxy, you should get the original index from your proxy model using mapToSource

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

            1 Reply Last reply
            0
            • R Offline
              R Offline
              rfringuello89
              wrote on last edited by
              #6

              Thank you! It works correctly now, but I can still see the empty rows when I delete them. I can solve re-selecting the model, is it the correct way?

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

                AFAIK you should not need to

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

                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