Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Selection error with QTableWidget
Qt 6.12 LTS is out! See what's new in the release blog

Selection error with QTableWidget

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 720 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.
  • ivanicyI Offline
    ivanicyI Offline
    ivanicy
    wrote on last edited by
    #1

    Hello! I am trying to delete the selected rows in a QTableWidget.

    When I select the first two rows by dragging like this:

    0_1530617200313_cee6ccc5-74df-4766-8df2-f8c9e6948193-image.png

    and I press the delete button which has this code:

    void PresetTourConfig::deleteSelectedPresetRows() {
        QModelIndexList selection( ui->tablePresets->selectionModel()->selectedRows() );
    
        for (int i = 0; i < selection.count(); i++) {
            QModelIndex index = selection.at(i);
            qDebug() << "Selection: " << index.data(0);
            ui->tablePresets->removeRow(index.row());
        }
    }
    

    The qDebug() shows this:

    Selection:  QVariant(QString, "01")
    Selection:  QVariant(QString, "03")
    

    Does anyone know why this happens?

    Thank you very much!!

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      because QModelIndex is not QPersistentModelIndex

      • selection will contain index(0,0) and index(1,0)
      • ui->tablePresets->removeRow(index.row()); removes the first row but index(1,0) did not update so it is still pointing at row number 2 that now contains "03"

      to solve, add std::sort(selection.begin(),selection.end(),[](const QModelIndex& left,const QModelIndex& right)->bool{return left.row()>right.row();}); before the for loop

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      ivanicyI 1 Reply Last reply
      6
      • VRoninV VRonin

        because QModelIndex is not QPersistentModelIndex

        • selection will contain index(0,0) and index(1,0)
        • ui->tablePresets->removeRow(index.row()); removes the first row but index(1,0) did not update so it is still pointing at row number 2 that now contains "03"

        to solve, add std::sort(selection.begin(),selection.end(),[](const QModelIndex& left,const QModelIndex& right)->bool{return left.row()>right.row();}); before the for loop

        ivanicyI Offline
        ivanicyI Offline
        ivanicy
        wrote on last edited by
        #3

        @VRonin Wow! Thank you very much! I think I couldn't be able to solve this on my self!

        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
        • Unsolved