C++ QTableWidget 与 QTableView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15290932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:14:09  来源:igfitidea点击:

QTableWidget vs QTableView

c++qtqtableviewqtablewidget

提问by Cool_Coder

I am new to this Model/View Framework of Qt. In my application I want to have 1000 X 1000 cells. There should be minimum memory requirement & it should be fast. I don't know what this Model terminology is for. But I have my own class which knows how to deal with the double variables stored in the table. Currently I am using QLineEdit's with a Validator to create the array of cells. But it was way too slow for cells > 50 X 50. So I decided to go the good old MS Excel way.

我是 Qt 模型/视图框架的新手。在我的应用程序中,我想要 1000 X 1000 个单元格。应该有最低内存要求并且应该很快。我不知道这个模型术语是什么。但是我有自己的类,它知道如何处理存储在表中的双变量。目前我使用 QLineEdit's 和一个验证器来创建单元格数组。但是对于 > 50 X 50 的单元格来说太慢了。所以我决定采用旧的 MS Excel 方式。

So which Widget should I use: QTableWidgetor QTableView?

那么我应该使用哪个 Widget:QTableWidgetQTableView?

And can anybody please explain in short what this Model/View framework is? I am not a Computer Science guy hence I am finding it tough to understand...

谁能简要解释一下这个模型/视图框架是什么?我不是计算机科学专业的人,因此我发现很难理解......

采纳答案by Phlucious

cmannett85's recommendation is a good one. Read the docs about a dozen times.

cmannett85 的推荐是不错的。阅读文档大约十几次。

Then, if performance and memory issues are your primary concern and you think you can out-perform the QTableWidget implementation, then a QTableViewinterface on top of a QAbstractTableModelor QStandardItemModelis what you're looking for.

然后,如果性能和内存问题是您的主要关注点,并且您认为您可以超越 QTableWidget 实现,那么QAbstractTableModelQStandardItemModel之上的QTableView接口就是您要寻找的。

Since you're new to Qt's model-view architecture, I'd recommend using the QStandardItemModel until you feel like you're getting the hang of it. If your performance still isn't good enough, avoid a lot of the memory duplication and wasted objects by implementing your custom model. Plus, get yourself a good textbookand read its chapter on the model-view framework about 12 times. That section alone was worth its weight in gold, imho.

由于您不熟悉 Qt 的模型视图架构,因此我建议您使用 QStandardItemModel,直到您觉得自己掌握了它的窍门。如果您的性能仍然不够好,请通过实现您的自定义模型来避免大量内存重复和浪费的对象。另外,给自己买一本好的教科书,并阅读它关于模型视图框架的章节大约 12 次。恕我直言,仅这一部分就值得用黄金来衡量。

Here are the basics for Qt's custom model-view framework:

以下是 Qt 自定义模型-视图框架的基础知识:

  • Your actual datais stored in a list/tree somewhere
  • The modelprovides a standard framework for queries to and edits for your data
  • Proxy modelsallow you to sort/filter your data without affecting the original model
  • The viewprovides a means to visually observeand interact withyour data
  • Delegates(often optional) tweak the appearance of your data and provide custom editors to the data
  • 您的实际数据存储在某个列表/树中
  • 模型为数据查询和编辑提供了标准框架
  • 代理模型允许您在不影响原始模型的情况下对数据进行排序/过滤
  • 视图提供了一种直观地观察交互数据
  • 委托(通常是可选的)调整数据的外观并为数据提供自定义编辑器

If you're feeling both cheap and brave, check out this excerpton implementing your own custom model. Work at it one function at a time and play with it as you go.

如果您觉得既便宜又勇敢,请查看有关实现您自己的自定义模型的摘录。一次只处理一个功能,然后边玩边玩。

回答by cmannett85

To understand the framework, start off with the documentationabout it. It starts slow, but becomes moderately extensive and covers most of the classes involved.

要了解该框架,请从有关它的文档开始。它开始缓慢,但变得适度广泛,涵盖了所涉及的大多数类。

QTableWidget or QTableView?

QTableWidget 还是 QTableView?

Once you have read the documentation you will see why this question doesn't really make any sense: a QTableWidgetusesa QTableViewto display the data. QTableWidget(along with QTreeWidget, etc.) uses the MVC framework, but it encapsulates it all to a handy package useful for most purposes, but if you need to do something different, you will have to crack it into it's component parts and reimplement the bits you need.

一旦您阅读了文档,您就会明白为什么这个问题没有任何意义:aQTableWidget使用aQTableView来显示数据。 QTableWidget(连同QTreeWidget等)使用 MVC 框架,但它将它全部封装到一个对大多数用途有用的方便的包中,但是如果你需要做一些不同的事情,你将不得不将它分解成它的组成部分并重新实现你的部分需要。