简体   繁体   English

如何将Qt图形场景项目保存在文件中并再次打开?

[英]How to save Qt graphic scene items in file and open it again?

I have no any experience with file streaming and trying to save scene items to file for later use. 我对文件流传输并没有尝试将场景项保存到文件中以备后用的经验。 I'm using PySide with Qt 4.7 lib. 我正在Qt 4.7 lib中使用PySide。 Do I have to use writeRawData for saving list of items or what ? 我必须使用writeRawData保存项目列表还是什么?

I would appreciate any help. 我将不胜感激任何帮助。 Some example would make my day. 举个例子,我会很开心。 Thanks. 谢谢。

There is no way to directly save a QGraphicsItem in a file (except in a pixmap) that you can reload. 无法将QGraphicsItem直接保存在可以重新加载的文件中(像素图除外)。

But, you can create a file that contains the description of each items in your view. 但是,您可以创建一个文件,其中包含视图中每个项目的描述。 What you have to do is to list every information (item type, position, shape, etc.) that define each item and store them in a file. 您要做的是列出定义每个项目的所有信息(项目类型,位置,形状等)并将其存储在文件中。

For example, collecting information for rect, ellipse and line items : 例如,收集有关rect,椭圆和订单项的信息:

scene = QGraphicsScene();
scene.addEllipse( 100, 100, 50, 50 )
scene.addRect( 200, 12, 120, 50 )
scene.addLine( 50, 70, 100, 400 )

for item in scene.items():
    if item.type() == QGraphicsEllipseItem().type():
        print "Ellipse",  item.rect()
    elif item.type() == QGraphicsRectItem().type():
        print "Rectangle",  item.rect()
    elif item.type() == QGraphicsLineItem().type():
        print "Line", item.line().p1(), item.line().p2()

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

相关问题 如何将鼠标事件(实例)保存在.txt文件中,然后再次将其作为实例打开 - how to save mouse event (instance) in .txt file and then open it as intance again 如何在txt文件中保存矩阵,然后在python中再次将其作为矩阵打开? - How can I save a matrix in a txt file and then open it again as a matrix in python? 我可以使用 python 打开 XML 文件,格式化,然后再次保存吗? - Can I use python to open an XML file, format it, then save it again? getSaveFileName,getOpenFileName向我显示要保存或打开的文件,但无法保存该文件或在python QT中打开该文件 - getSaveFileName , getOpenFileName shows me the file to save or open, but cannot save the file or open the file in python QT 如何再次打开相同的pyqt文件 - How to open same pyqt file again 如何打开文件并另存为csv - How to open file and save as csv 如何再次将TreeView中的文件另存为json文件? - How can I save the file in TreeView as a json file again? 如何将 pygame 场景保存为 jpeg? - How to save pygame scene as jpeg? 如何将Qlistwidget项目保存在数据库或txt文件中 - How to save Qlistwidget items in database or in txt file 打开另一个py文件的Tkinter图形形式(没有文件,只有图形形式) - Open Tkinter graphic form of another py file (no file, only graphic form)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM