QTableView clearSelection fails with ASSERT: "!isEmpty()" in file /usr/include/qt4/QtCore/qlist.h, line 282

4.1k views Asked by At

I have this class:

class MyWidget : public QWidget {
Q_OBJECT
public:
...
public slot:
void select( const QItemSelection& selected, const QItemSelection& deselected);
private:
QTableView* view;
MyModelClass* model;
}

In my cunstructor:

view->setEditTriggers( QAbstractItemView::NoEditTriggers );
view->setSelectionMode( QAbstractItemView::SelectionMode::SingleSelection );
view->setSelectionBehavior( QAbstractItemView::SelectionBehavior::SelectRows );
connect( view->selectionModel( ), SIGNAL( selectionChanged ( const QItemSelection&, const QItemSelection& ) ), this, SLOT( select( const QItemSelection&, const QItemSelection& ) ) );
// and few other things

In my slot implementation:

void MyWidget::select( const QItemSelection& selected, const QItemSelection& deselected ) {
//... doing few things
// at the end:
view->clearSelection(); 
// tried view->selectionModel()->clear() and view->selectionModel()->clearSelection() too
// but got the same result
}

It compiles just right, but when I run and do a selection it crashes at the end with this error message:

ASSERT: "!isEmpty()" in file /usr/include/qt4/QtCore/qlist.h, line 282

I tried other tricks as well: Reimplementing showEvent method and calling clearSelection from that context, but didn't help :(

My Qt version is 4.8.1. Any help would be nice. Thanks in advance.

1

There are 1 answers

0
Attila Horvath On BEST ANSWER

Well... it was a pretty dumb mistake. As vahancho said, it implied a recursive call. I had to add a line to my select function to skip the second ( recursive ) call.

if( selected.indexes( ).empty( ) ) return;

It was my big mistake, but maybe it helps others too.