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

标签 python qt qt4 pyqt pyside

我对文件流和尝试将场景项目保存到文件以供以后使用没有任何经验。 我正在使用 PySide 和 Qt 4.7 lib。我是否必须使用 writeRawData 来保存项目列表或什么?

如果有任何帮助,我将不胜感激。一些例子会让我很开心。谢谢。

最佳答案

没有办法直接将 QGraphicsItem 保存在可以重新加载的文件中(像素图中除外)。

但是,您可以创建一个文件,其中包含 View 中每个项目的描述。您要做的就是列出定义每个项目的所有信息(项目类型、位置、形状等)并将它们存储在文件中。

例如,收集矩形、椭圆形和线条项的信息:​​

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()

关于python - 如何将Qt图形场景项保存在文件中并再次打开它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15156465/

相关文章:

python - 检查几列中的日期并在 python 中创建一个新列

python - 带权重的欧氏距离

c++ - 终止在不同 QThread 中运行的 QProcess

c++ - QObject::connect: 无法连接 (null)::mySignal() 到 (null)::mySlot()

c++ - 使用 Visual C++ 2010 构建 Qt 4.5

.net - QT4 与 .Net 相比如何

python - CNTK序列模型错误: Different minibatch layouts detected

c++ - 将长字符流写入文本文件 QT C++

c++ - QSharedPointer VS std::tr1::shared_ptr VS boost::tr1::shared_ptr

python - Django/python : Understanding paths in django