将包含子元素的整个QGraphicScene保存到一个文件中
Save entire QGraphicScene with sub elements to a file
我有一个自定义的QGraphicScene,上面有一些QGraphicsItem QGraphicsLine等。与qt的diagramscene示例中显示的方式相同。有没有一种方法可以将它保存到文件中,然后在需要时打开并重复使用或修改它?在PyQt4上工作,但也欢迎使用qt4相关性的答案(c++而不是python)。
这本质上是保存和重新创建。
##want to save the scene
elif key == QtCore.Qt.Key_S and modifier == QtCore.Qt.ControlModifier:
#saving all the items in the current scene
self.arrowItemVec=[]
self.allItems=self.scene.items()
#for arrow we need the start and end item to recreate
#for now only saving the names
for item in self.allItems:
if 'Arrow'==item.__class__.__name__:
self.arrowItemVec.append([item.startItem().name,item.endItem().name])
elif key == QtCore.Qt.Key_L and modifier == QtCore.Qt.ControlModifier:
#recreating all the saved items
#if the scene is cleared before creating new items. we
#loose the old item positions
#essentially we need to save the complete scene
#detect what item it was and then recreate it
newItemVec=[]
for item in self.allItems:
if 'DiagramItem'==item.__class__.__name__:
newItem=DiagramItem(diagramType=item.diagramType,contextMenu=None)
newItem.name=item.name
newItem.signaller.showChild.connect(self.showChildGraph)
newItemVec.append(newItem)
newItem.setPos(item.pos().x()+10,item.pos().y()+10)
#else:
#print(item.__class__.__name__)
count=0
for item in self.allItems:
if 'Arrow'==item.__class__.__name__:
for newItem in newItemVec:
if newItem.name==self.arrowItemVec[count][0]:
startItem=newItem
if newItem.name==self.arrowItemVec[count][1]:
endItem=newItem
count+=1
newArrow=Arrow(startItem,endItem)
newArrow.setZValue(1000)
newItemVec.append(newArrow)
self.scene.clear()
self.scene.addGrid()
for item in newItemVec:
self.scene.addItem(item)
没有直接的方法可以将QGraphicsItem或派生类保存到文件中。
您需要做的是迭代每个项目并保存其属性,然后可以读回这些属性并重新创建场景。
相关文章:
- 挂起和取消挂起一个文件DLL
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- C++试图读取一个文件并输出到另一个文本文件
- 如何从另一个文件继承私有成员变量和公共函数
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 如何将class.cpp和class.hpp编译为一个.o文件
- 如何在 Gnuplot 中分别绘制 2 个文件数据?我有一个文件"sin.txt",另一个文件"cos.txt",我想将它们分别绘制在一个图表上
- 如何从整个磁盘中排除要装载的一个文件
- 如何将模板和非模板函数放在一个文件中
- 如何在安卓工作室中将c ++头文件从一个文件夹导入到另一个文件夹?
- 使用 scope 运算符 (::) 引用另一个文件中的类
- JNI,使用两个 .so 文件时出错,其中一个文件需要另一个文件
- 为什么我不能从同一目录中的另一个文件中 #include 我的类?
- 在 c++ 中打开文件、删除标点符号并追加到另一个文件中
- C++ 如何将两个 makefile 对象目标规则(位于另一个文件夹中)合并到一个目标/规则中?
- 是否可以将子进程的 stdout 重定向到父进程中的另一个文件?
- c++ 提升如果 .extension() == " "这意味着这个文件是一个文件夹?
- 是否可以在 c++ 中创建一个文件并为其提供属性以将其隐藏?(在 Ubuntu 上)
- 我需要编写一个程序来读取一个文件,该文件将输出所有唯一的整数,如果已经看到整数,它将被关闭
- 如何在标准c ++中流式传输/读取二进制文件的中间部分并写入另一个文件?