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. [QTableView remove selected rows?
Forum Updated to NodeBB v4.3 + New Features

[QTableView remove selected rows?

Scheduled Pinned Locked Moved Solved General and Desktop
qt4.8
27 Posts 4 Posters 29.2k Views 1 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.
  • P Offline
    P Offline
    Peppy
    wrote on last edited by A Former User
    #1

    Hi guys, I have issue with QTableView... I can't remove selected items from the table correctly ... I got two methods:

    @
    for(int i=...)
    {
    view->model()->removeRow(i);
    @

    Which works fine if I pick up each line separately, or this one:
    @
    for(int i=...)
    {
    view->model()->removeRow(view->currentIndex().row());
    @
    which works good only if I use Drag'n'Drop technique...

    Both works only in specific situation of selecting lines, but I can't reach the correct answer how to remove selected lines (by picking up, or by Drag'n'Drop)....How to remove lines from QTableView??

    1 Reply Last reply
    0
    • H Offline
      H Offline
      Hostel
      wrote on last edited by
      #2

      Maybe you need a "QTableView::selectedIndexes()":http://qt-project.org/doc/qt-4.8/qtableview.html#selectedIndexes.

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Peppy
        wrote on last edited by
        #3

        Yea, I am using that one.

        But I have solved that by decrementing (and removing from the end of the table view, not from the beginning):
        I shouldn't increment over the list and remove the first items because table was shifting rows after I have removed one - and that was making issues. So I had to write decrementing cycle and removing from the end of the table view - after shifting I am able to remove the correct row.

        From:
        @
        QList<int> rowIndexes;
        for (int i = 0; i < rowIndexes.count(); ++i)
        tableView->model()->removeRow(rowsIndexes[i]); // shifting rows after removing causes "bad removing"
        @

        To:
        @
        QList<int> rowIndexes;
        for (int i = rowIndexes.count(); i >= 0; --i)
        tableView->model()->removeRow(rowsIndexes[i]);
        @

        V 1 Reply Last reply
        0
        • P Peppy

          Yea, I am using that one.

          But I have solved that by decrementing (and removing from the end of the table view, not from the beginning):
          I shouldn't increment over the list and remove the first items because table was shifting rows after I have removed one - and that was making issues. So I had to write decrementing cycle and removing from the end of the table view - after shifting I am able to remove the correct row.

          From:
          @
          QList<int> rowIndexes;
          for (int i = 0; i < rowIndexes.count(); ++i)
          tableView->model()->removeRow(rowsIndexes[i]); // shifting rows after removing causes "bad removing"
          @

          To:
          @
          QList<int> rowIndexes;
          for (int i = rowIndexes.count(); i >= 0; --i)
          tableView->model()->removeRow(rowsIndexes[i]);
          @

          V Offline
          V Offline
          veera
          wrote on last edited by
          #4

          @Peppy
          Hi All,
          We can do using this push button method for deleting row of table.
          void MainWindow::on_delete_record_clicked()
          {

           QSqlQuery* query=new QSqlQuery();
          
          
          QString str4;
          str4 = ui->lineEdit_5->text();
          

          // int id=str4.toInt();
          query->addBindValue(str4);
          /*
          QString str3;
          str3 = ui->lineEdit_4->text();
          query->addBindValue(str3);
          */
          query->prepare("DELETE FROM entry where id = 'id' ");
          // query->addBindValue(":id");
          //ui->tablewidget->removeRow(ui->tablewidget->currentRow());

          query->exec();
          if (query->exec())
          {
          
              QMessageBox::critical(this,tr("Delete"),tr("Deleted"));
              //db.close();
          
          }
          
          1 Reply Last reply
          0
          • V Offline
            V Offline
            veera
            wrote on last edited by
            #5

            Hi All,
            With the above code i could not able to remove the selected rows from the QTableview is interface with database conncetion......please any one guide me

            1 Reply Last reply
            0
            • V Offline
              V Offline
              veera
              wrote on last edited by
              #6

              I am not able to delete by selecting entire row from the Qtableview using delete push button ...give me some examples

              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by VRonin
                #7
                // get the selected indexes
                QModelIndexList selectedIndexes = tableView->selectionModel()->selectedIndexes();
                // make sure you are not deleting from a tree
                Q_ASSERT(std::equal(selectedIndexes.constBegin()+1,selectedIndexes.constEnd(),selectedIndexes.constBegin(),[](const QModelIndex& a,const QModelIndex& b)->bool{return a.parent()==b.parent();}));
                // sort from bottom to top
                std::sort( selectedIndexes.begin(),selectedIndexes.end(),[](const QModelIndex& a,const QModelIndex& b)->bool{return b.row()<a.row();});
                QSet<int> deletedRows; // keep a record of rows already deleted
                for(auto i = selectedIndexes.constBegin();i!=selectedIndexes.constEnd();++i){
                if(deletedRows.contains(i->row())) 
                continue; //row deleted already, move on
                if(tableView->model()->removeRow(i->row(),i->parent())) // try to delete the row
                deletedRows << i->row(); // if successful store the row as deleted
                }
                

                "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

                V 1 Reply Last reply
                2
                • VRoninV VRonin
                  // get the selected indexes
                  QModelIndexList selectedIndexes = tableView->selectionModel()->selectedIndexes();
                  // make sure you are not deleting from a tree
                  Q_ASSERT(std::equal(selectedIndexes.constBegin()+1,selectedIndexes.constEnd(),selectedIndexes.constBegin(),[](const QModelIndex& a,const QModelIndex& b)->bool{return a.parent()==b.parent();}));
                  // sort from bottom to top
                  std::sort( selectedIndexes.begin(),selectedIndexes.end(),[](const QModelIndex& a,const QModelIndex& b)->bool{return b.row()<a.row();});
                  QSet<int> deletedRows; // keep a record of rows already deleted
                  for(auto i = selectedIndexes.constBegin();i!=selectedIndexes.constEnd();++i){
                  if(deletedRows.contains(i->row())) 
                  continue; //row deleted already, move on
                  if(tableView->model()->removeRow(i->row(),i->parent())) // try to delete the row
                  deletedRows << i->row(); // if successful store the row as deleted
                  }
                  
                  V Offline
                  V Offline
                  veera
                  wrote on last edited by
                  #8

                  @VRonin

                  I got selectedIndexes is 5 ITEMS while debugging my code "deletedRows" is 0 after that my selected row is remains same only it is not deleted .....my QTableview is connected with database.....

                  VRoninV 1 Reply Last reply
                  0
                  • V veera

                    @VRonin

                    I got selectedIndexes is 5 ITEMS while debugging my code "deletedRows" is 0 after that my selected row is remains same only it is not deleted .....my QTableview is connected with database.....

                    VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by
                    #9

                    @veera said in [QTableView remove selected rows?:

                    QTableview is connected with database

                    What model are you using?

                    "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

                    V 1 Reply Last reply
                    0
                    • VRoninV VRonin

                      @veera said in [QTableView remove selected rows?:

                      QTableview is connected with database

                      What model are you using?

                      V Offline
                      V Offline
                      veera
                      wrote on last edited by
                      #10

                      @VRonin
                      I am using this model
                      QSqlQueryModel *model = new QSqlQueryModel;

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

                        from http://doc.qt.io/qt-5/qsqlquerymodel.html:

                        The QSqlQueryModel class provides a read-only data model for SQL result sets.

                        Since it's read only it does not allow you to delete rows

                        "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

                        1 Reply Last reply
                        0
                        • V Offline
                          V Offline
                          veera
                          wrote on last edited by
                          #12

                          Then which model i need to use and how to do it please give me some examples ........

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

                            either QSqlTableModel (be carefull with this one, read the docs) or you manually populate a QStandardItemModel from the data you get from a QSqlQuery

                            "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

                            1 Reply Last reply
                            0
                            • V Offline
                              V Offline
                              veera
                              wrote on last edited by
                              #14

                              give me some examples so that i can understand it better

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

                                QStandardItemModel + QSqlQuery https://forum.qt.io/topic/76135/qsqlrelationaltablemodel-and-complex-queries/21

                                QSqlTableModel http://doc.qt.io/qt-5/qsqltablemodel.html#details

                                "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

                                1 Reply Last reply
                                3
                                • V Offline
                                  V Offline
                                  veera
                                  wrote on last edited by
                                  #16

                                  Its not happening ....... i have selected one and tried but its not deleting and tried many rows also not deleting my database is MSSQL in Ubuntu.....

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

                                    30 minutes before giving up?

                                    create a dummy table in your database (so even in case of a disaster you won't lose anything important) and try with QSqlTableModel

                                    "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

                                    1 Reply Last reply
                                    0
                                    • V Offline
                                      V Offline
                                      veera
                                      wrote on last edited by
                                      #18

                                      I want to something this type of query
                                      query->prepare("DELETE FROM Info WHERE ID = ?");
                                      query->addBindValue(ID);
                                      for deleting of entire row by selecting a particular row...........

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

                                        QSqlTableModel or QSqlRelationalTableModel does that for you if you can live with simple queries. if you have convoluted joins then you'll have to connect to the model rowsRemoved or rowsAboutToBeRemoved signal and prepare the query manually

                                        "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

                                        V 1 Reply Last reply
                                        1
                                        • VRoninV VRonin

                                          QSqlTableModel or QSqlRelationalTableModel does that for you if you can live with simple queries. if you have convoluted joins then you'll have to connect to the model rowsRemoved or rowsAboutToBeRemoved signal and prepare the query manually

                                          V Offline
                                          V Offline
                                          veera
                                          wrote on last edited by
                                          #20

                                          @VRonin
                                          I am a beginner of MS SQL so please give me examples

                                          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