我应该如何将数据类链接到我的 GUI 代码(以在 C++ 中显示对象的属性)?
我有一个类(在 C++ 中),称为 Data
,在代码运行时它有数千个实例(对象)。 我有一个小部件(在 Qt 中),将其称为 DataWidget
,它显示对象的属性。 为了快速构建小部件,我只需将对象属性写入文件并让小部件解析文件的属性 - 这种方法有效,但不可扩展或不美观。
更清楚地说,我的要求是:
1 - DataWidget
应该能够一次显示多个不同的 Data
对象的属性
2 - DataWidget
应该能够每秒显示数千个 Data
对象
3 - DataWidget
应与生成新 Data
对象的代码一起运行
4 - 每个Data
对象需要永久保存到文件/数据库
当前,GUI已创建,DataWidget
已创建,然后实验运行并生成数千个 Data 对象(定期将其中一些写入文件)。 实验运行后,DataWidget
显示写入文件的最后一个 Data
对象(它们被写入 XML 文件)。
使用我当前的文件方法,我可以通过在实验运行后获取多个文件来满足(1)。 由于实验与 DataWidget
无关,因此没有并发性,因此在添加通知 DataWidget 的信号之前我无法执行(3)
存在一个新文件。
我没有继续采用这种方法有两个原因: 首先,即使文件没有立即写入磁盘,我也无法想象这种方法是可扩展的,除非我实现了缓存系统 - 但是,这似乎是我在重新发明轮子? 其次,Data
是图形数据结构的包装器,我使用 Graphml(通过 Boost Graph Library,即 write_graphml())将该结构写入 XML 文件,并且要使用 Boost 的 read_graphml() 读回结构,需要我将文件读回到 Data
对象中......这意味着程序的实验部分编码了将 XML 对象转换为 XML,将 XML 写入文件(但希望在内存中而不是磁盘中),然后 DataWidget 从文件中读取 XML 并将其解码为对象!
在我看来,我应该使用一个可以处理所有缓存等的数据库。此外,我似乎应该能够跳过文件/数据库步骤并将 Data
传递给 DataWidget
在程序中(也许向其传递对Data
列表的引用)。 然而,我还想将Data
保存到文件到文件/数据库步骤并不是完全没有意义的 - 我只是在错误的时间以错误的方式使用它。
考虑到我的要求,更好的方法是什么?
是否有处理和显示此类数据的通用资源和/或指南?
I have a class (in C++), call it Data
, that has thousands of instances (objects) when the code is run. I have a widget (in Qt), call it DataWidget
that displays attributes of the objects. To rapidly build the widget I simply wrote the object attributes to a file and had the widget parse the file for the attributes - this approach works, but isn't scalable or pretty.
To be more clear my requirements are:
1 - DataWidget
should be able to display multiple, different, Data
object's attributes at a time
2 - DataWidget
should be able to display thousands of Data
objects per second
3 - DataWidget
should be run along side the code that generates new Data
objects
4 - each Data
object needs to be permanently saved to file/database
Currently, the GUI is created and the DataWidget
is created then the experiment runs and generates thousands of Data
objects (periodically writing some of them to file). After the experiment runs the DataWidget
displays the last Data
object written to file (they are written to XML files).
With my current file approach I can satisfy (1) by grabbing more than one file after the experiment runs. Since the experiment isn't tied to DataWidget
, there is no concurrency, so I can't do (3) until I add a signal that informs the DataWidget
that a new file exists.
I haven't moved forward with this approach for 2 reasons:
Firstly, even though the files aren't immediately written to disk, I can't imagine that this method is scalable unless I implement a caching system - but, this seems like I'm reinvent the wheel? Secondly, Data
is a wrapper for a graph data-structure and I'm using Graphml (via Boost Graph Library i.e. write_graphml()) to write the structure to XML files, and to read the structure back in with Boost's read_graphml() requires me to read the file back into a Data
object ... which means the experiment portion of the program encodes the object into XML, writes the XML to a file (but hopefully in memory and not to disk), then the DataWidget
reads the XML from a file and decodes it into an object!
It seems to me like I should be using a database which would handle all the caching etc. Moreover, it seems like I should be able to skip the file/database step and pass the Data
to the DataWidget
in the program (perhaps pass it a reference to a list of Data
). Yet, I also want to save the Data
to file to the file/database step isn't entirely pointless - I'm just using it in the wrong way at the wrong time.
What is the better approach given my requirements?
Are there any general resources and/or guidelines for handling and displaying data like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我发现你正在使用 Qt。 这很好,因为 Qt 4.0 及更高版本包含强大的模型/视图框架。 我认为这就是你想要的。
模型/视图
基本上,拥有您的
数据
类继承并实现 QAbstractItemModel 或不同的 Qt Model 类,具体取决于您想要的模型类型。 然后设置您的视图小部件(很可能是 QListView)以使用Data
作为其模型。他们的网站上有很多示例,该解决方案可以很好地适应大型数据集。
补充:来自 labs.trolltech.com 的模型测试代码非常方便:
http:// /labs.trolltech.com/page/Projects/Itemview/Modeltest
I see you're using Qt. This is good because Qt 4.0 and later includes a powerful model/view framework. And I think this is what you want.
Model/View
Basically, have your
Data
class inherit and implement QAbstractItemModel, or a different Qt Model class, depending on the kind of model you want. Then set your view widget (most likely a QListView) to useData
for its model.There are lots of examples at their site and this solution scales nicely with large data sets.
Added: This model test code from labs.trolltech.com comes in real handy:
http://labs.trolltech.com/page/Projects/Itemview/Modeltest
如果您需要显示如此多快速变化的数据,那么拥有中间文件或数据库会减慢速度并可能成为瓶颈。 我认为Widget应该直接从内存中读取新生成的数据。 但这并不妨碍您将数据存储在文件或数据库中,它可以在单独的线程/进程中完成。
If you need to display that much rapidly changing data, having an intermediate file or database will slow it down and likely become the bottleneck. I think the Widget should read the newly generated data directly from memory. This doesn't prevent you from storing the data in a file or database though, it can be done in a separate thread/process.
如果所有数据项都适合内存,我会说将它们放入向量/列表中,并将对该数据项的引用传递给 DataWidget。 当需要保存它们时,传递对序列化方法的引用。 然后你的实验只是填充数据结构以供其他进程使用。
If all of the data items will fit in memory, I'd say put them in a vector/list, and pass a reference to that to the DataWidget. When it's time to save them, pass a reference to your serializing method. Then your experiment just populates the data structure for the other processes to use.