4

We usually set view's model in Qt like this: view->setModel(model);

But is there any way to remove a model from view? I mean literally leave a view empty like it was just created and there was not any model set to it yet.

If you ask me a reason of my desire, I have a pretty much similar case as in this guy's post. And when first view has no selection or it is empty/invalid/whatever, I want to make the second view show literally nothing: no headers, columns, rubbish data. Removing a model from the view seems to be pretty reasonable in that case.

I've tried a dirty hack: *view = QTableView(); But Qt took care about such evil things and made operator= private.

Nikolai Shalakin
  • 1,349
  • 2
  • 13
  • 25
  • Does [`view->setModel(nullptr)`](http://doc.qt.io/qt-5/qabstractitemview.html#setModel) not work? – G.M. Oct 08 '17 at 11:02

1 Answers1

7

From the source of QAbstractItemView::setModel():

d->model = (model ? model : QAbstractItemModelPrivate::staticEmptyModel());

It looks like if you pass a null pointer, it will internally use some dummy model fallback. So null pointers are supported and that's a valid way to go to "unset" the current model.

dtech
  • 47,916
  • 17
  • 112
  • 190