Select behavior in Qtableview when remove item.
-
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? -
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
-
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? -
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.
-
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 -
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.
-
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? -
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()