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. Select behavior in Qtableview when remove item.
Forum Updated to NodeBB v4.3 + New Features

Select behavior in Qtableview when remove item.

Scheduled Pinned Locked Moved General and Desktop
8 Posts 5 Posters 5.0k 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.
  • L Offline
    L Offline
    Landy
    wrote on last edited by
    #1

    Hi guys,

    when i select an item in QTableView, for example, index(1, 0).
    after that, i removed the item selected, who's row is 1. then, the next item will be selected automaticly.
    i wanna disable this featue.
    so, is there any way to do this?

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jmelbye
      wrote on last edited by
      #2

      It looks like you can get the selection model from your view object, and call clearSelection() on it.

      @myView->selectionModel()->clearSelection()@

      "http://doc.qt.digia.com/qt/qabstractitemview.html#selectionModel":http://doc.qt.digia.com/qt/qabstractitemview.html#selectionModel

      "http://doc.qt.digia.com/qt/qitemselectionmodel.html#clearSelection":http://doc.qt.digia.com/qt/qitemselectionmodel.html#clearSelection

      1 Reply Last reply
      0
      • L Offline
        L Offline
        Landy
        wrote on last edited by
        #3

        Hi jmelbye,

        thanks for ur reply.
        but i don't think your solution can work since the next item will be selected a little later after one item is removed.
        so , anyway, operation clearSelection will be executed before the next item is selected.
        actually, what i want is to disable the feature which will switch selection after current selection is removed.
        do u have any good idea?

        1 Reply Last reply
        0
        • C Offline
          C Offline
          ChrisW67
          wrote on last edited by
          #4

          This works:
          @
          class MainWindow: public QMainWindow {
          Q_OBJECT
          public:
          MainWindow(QWidget *p = 0): QMainWindow(p) {
          QWidget *central = new QWidget(this);
          view = new QTableView(this);
          QPushButton *button = new QPushButton("Delete", this);
          connect(button, SIGNAL(clicked()), SLOT(doit()));

              model = new QStandardItemModel(3,3,this);
              for (int r = 0; r < 3; ++r)
                  for (int c = 0; c < 3; ++c)
                      model->setItem(r, c, new QStandardItem(QString("%1,%2").arg(r).arg(c)));
              view->setModel(model);
              view->setSelectionMode(QAbstractItemView::SingleSelection);
              view->setSelectionBehavior(QAbstractItemView::SelectRows);
          
              QVBoxLayout *layout = new QVBoxLayout(central);
              layout->addWidget(view);
              layout->addWidget(button);
          
              central->setLayout(layout);
              setCentralWidget(central);
          }
          

          public slots:
          void doit() {
          QModelIndexList indexes = view->selectionModel()->selectedRows();
          qDebug() << "Before" << indexes;
          if (indexes.count() > 0) {
          const QModelIndex index = view->selectionModel()->selectedRows().at(0);
          model->removeRow(index.row());
          view->selectionModel()->clearSelection();
          }
          qDebug() << "After" << view->selectionModel()->selectedRows();

          }
          

          private:
          QTableView *view;
          QStandardItemModel *model;
          };
          @
          After deleting a row there is no selection (visible or not)

          Why don't you show us what you are actually doing and perhaps we can help.

          1 Reply Last reply
          0
          • JeroentjehomeJ Offline
            JeroentjehomeJ Offline
            Jeroentjehome
            wrote on last edited by
            #5

            Only a quick word, but don't forget the "beginRemoveRows" and "endRemoveRows" in the doit() function!! That gives the view information about what happens in side the model and keeps your view/model concept in sync!
            Greetz

            Greetz, Jeroen

            1 Reply Last reply
            0
            • C Offline
              C Offline
              ChrisW67
              wrote on last edited by
              #6

              Those functions are neither needed nor accessible in the doit() function: the example code is not implementing a model, just manipulating one. These protected functions are called in the model code before it adjusts row/column counts and cause the emission of the corresponding signals from the model.

              1 Reply Last reply
              0
              • L Offline
                L Offline
                Landy
                wrote on last edited by
                #7

                Hi all,
                thanks for your help.
                but i don't think it works well in my code.
                actually, the problem in my project is that: when i remove an item from model, even i cleared selection, table will auto make another item be selected after an item is removed.
                it seems that it's a widget feature. i mean, when an item is removed, selected index will be updated automaticly.
                so, what i wanna is that, is there any way to disable the feature?

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  bprovencher
                  wrote on last edited by
                  #8

                  If somebody else encounter this issue here is how I fixed it: First block selection model signal, delete the row, re-enable selection model signal, clear selection:

                      tableView.selectionModel().blockSignals(True)
                      tableView_model.delete_row(row_idx)
                      tableView.selectionModel().blockSignals(False)
                      tableView.selectionModel().clearSelection()
                  

                  Here is the delete_row code:

                  def delete_row(self, row_idx: int):
                          self.beginRemoveRows(QModelIndex(), row_idx, row_idx)
                          del self._models_list[row_idx]
                          self.endRemoveRows()
                  
                  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