简体   繁体   English

Qt:从视图中删除模型

[英]Qt: remove model from view

We usually set view's model in Qt like this: view->setModel(model); 我们通常像这样在Qt中设置视图的模型: 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(); 我尝试了一个肮脏的hack: *view = QTableView(); But Qt took care about such evil things and made operator= private. 但是Qt照顾了这些邪恶的事情,并把operator=私有了。

From the source of QAbstractItemView::setModel() : 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. 因此,支持空指针,这是“重置”当前模型​​的有效方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM