Qt c++ - 克隆对象
我创建了自己的类,它包含向量的向量,每个位置包含一个自定义的 QGraphcsItem (图表)。我正在尝试创建撤消/重做功能,我采取的方法是使用堆栈来保存图表的每个状态。我正在考虑的两种方法是:
创建顶级 QVector 的克隆,这反过来需要克隆其中的所有子向量,然后需要克隆所有 QGraphicsItems。我想我可以循环遍历每个维度,为外部循环创建 QVector,然后克隆内部循环上的 QGraphicsItem,除非可以简单地克隆基本 QVector (并且本质上克隆内部的所有内容 - 目前内部的所有内容都是指针)。
本质上是根据图表的当前状态构建一个新的 QVector,而不是直接克隆图表。每个 QGraphicsItem 都有 get/set 函数来检索我需要的任何属性,因此我可以创建一个新的顶级 QVector,外部循环为每行创建一个新的 QVector,然后内部循环创建新的 QGraphicsItems,并设置原始的属性 本质上是根据图表
有没有一种简单的方法可以只克隆一个项目,从而克隆里面的所有内容?或者我基本上必须实现自己的克隆功能并“重建”当前状态?
I've created my own class that holds a Vector of Vectors, each position holding a custom QGraphcsItem (a chart). I'm attempting to create undo / redo functionality, and the approach I'm taking is to using a stack to hold each state of the chart. The two approaches I'm thinking are:
Create a clone of the top level QVector, which in turn would need to clone all the sub-Vectors inside it, which would then need to clone all the QGraphicsItems. I figure I can loop through each dimension, create the QVector for the outer loop, then cloning the QGraphicsItem on the inner loop, unless it's possible to just simply clone the base QVector (and inherently clone everything inside - currently everything inside are pointers).
Essentially build a new QVector out of the current state of the chart instead of directly cloning the chart. Each QGraphicsItem has get/set functions to retrieve any properties I need, so I could just create a new top level QVector, outer loop to create a new QVector for each row, then inner loop creating new QGraphicsItems, and setting the properties of the original chart.
Is there a straightforward way to just clone one item, which clones everything inside? Or do I basically have to implement my own cloning function and "rebuild" the current state?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢您的评论 - 我从来不知道 Qt 撤消/重做实现。我最终实现了自己的以获得更多控制权。
为了供其他遇到类似问题的人将来参考,我首先尝试创建一个新的 2D 矢量来保存图表的“状态”,并填充 QGraphicsItems。这种方法一开始是有效的,直到图表尺寸太大,每次更新图表时都会生成 10,000 个新的 QGraphicsItems,并且删除当前的 10,000 个并添加新的 10,000 个速度慢得难以忍受。
我将其更改为存储一个 2DVector,该 2DVector 保存图表状态的属性(仅 QColor、字符串等),而不是 QGraphicItems 本身。这使我能够只更新图表,而不是完全重新创建图表。
Thanks for the comments - I never knew about the Qt undo/redo implementation. I ended up implementing my own for a bit more control.
For future reference for others with a similar issue, I first tried creating a new 2D Vector to hold the "state" of the chart, filled with QGraphicsItems. This worked at first, until the chart was such a big size that generating 10,000 new QGraphicsItems every time the chart is updated, as well as removing the current 10,000 and adding the new 10,000 was unbearably slow.
I changed it up to instead store a 2DVector that held properties (just QColor, a string, etc) of the chart state, rather than the QGraphicItems themselves. This allowed me to just update the chart, rather than completely re-create the chart.