简体   繁体   English

读取QGraphicsScene并将其写入二进制文件

[英]read and write QGraphicsScene to a binary file

In my application is written in Qt, I have a QGraphicsScene. 在我用Qt编写的应用程序中,我有一个QGraphicsScene。 In this QgraphicsScene there is an Image and some items that draw by user. 在此QgraphicsScene中,有一个图像和一些由用户绘制的项目。 I want to save this QgraphicsScene with whole of stuff on it. 我想保存所有内容并保存此QgraphicsScene。

for example I have this stuff: 例如我有这个东西:

QGraphicsPixmapItem* image;
QPointF point;
ChromosomeShape::type pointType;
QGraphicsItem *item;

when i save these to file, it seems there is no problem 当我将这些保存到文件中时,似乎没有问题

but when I want to load ( datastream >> image; ), I got some error about "no match 'operator >>', qdatastream and QGraphicsPixmapItem*" 但是当我要加载( datastream >> image; )时,出现一些关于"no match 'operator >>', qdatastream and QGraphicsPixmapItem*"

but I don't want to overload operaptor>> for QGraphicsPixmapItem, and really I don't know how. 但我不想为QGraphicsPixmapItem重载operopertor >>,而且我真的不知道如何。

Q: is there any way I can do this? 问:有什么办法可以做到?

any idea is well appreciated. 任何想法都很好。

Read and write a QGraphicsScene from and to a binary file is not provided by Qt, and will be extremely long and difficult to do by yourself if you are not used to serialization with abstract classes and tree hierarchies. Qt不提供在二进制文件之间读取和写入QGraphicsScene的功能,如果您不习惯使用抽象类和树层次结构进行序列化,则将非常漫长并且很难自己完成。 This will be your best friend if you want to give it a try. 如果您想尝试一下, 将是您最好的朋友。 In the case of QGraphicsScene, you are in this situation 对于QGraphicsScene,您处于这种情况

In your case, you probably only want to read/write a small subset of QGraphicsScene that you know in advance, so may be feasible though. 在您的情况下,您可能只想读/写一个事先知道的QGraphicsScene小子集,因此可能是可行的。 For instance, just start as you did by trying to read/write a QGraphicsPixmapItem . 例如,通过尝试读取/写入QGraphicsPixmapItem就像开始一样开始。 Hence, you do have to implement yourself the two methods: 因此,您必须实现两个方法:

QDataStream &operator<<(QDataStream &, const QGraphicsPixmapItem &);
QDataStream &operator>>(QDataStream &, QGraphicsPixmapItem &);

That don't exist yet. 那还不存在。 What you've probably written when you said "when I save these to file, it seems there is no problem" is only the address of your QGraphicsPixmapItem, ie just a 32 or 64bit number, which is useless to be able to read it again ;-) More specifically, you probably did: 当您说“当我将它们保存到文件中时,似乎没有问题”时,您可能写的只是QGraphicsPixmapItem的地址 ,即只有32位或64位的数字,无法再次读取它;-)更具体地说,您可能做了:

datastream << image; // writing a QGraphicsPixmapItem*, useless

instead of: 代替:

datastream << *image; // writing a QGraphicsPixmapItem, useful but not provided by Qt

Hopefully, this should not be too complicated to write, since Qt already provides the QPixmap and QTransform serialization functions. 希望这不会太复杂,因为Qt已经提供了QPixmap和QTransform序列化功能。 Try something like: 尝试类似:

QDataStream &operator<<(QDataStream & out, const QGraphicsPixmapItem & item)
{
    out << item.transform() << item.pixmap();
    return out;
}

QDataStream &operator>>(QDataStream & in, QGraphicsPixmapItem & item)
{
    QTransform t;
    QPixmap p;
    in >> t >> p;
    item.setTransform(t);
    item.setPixmap(p);
    return in;
}

And then you can save you QGraphicsPixmapItem * with: 然后您可以使用以下命令保存QGraphicsPixmapItem *

datastream << *image;

And load it with: 并加载:

QGraphicsPixmapItem * image = new QGraphicsPixmapItem();
datastream >> *image;

If you need to save other properties of your QGraphicsPixmapItem (such as isActive , isVisible , acceptDrop , offset , shapeMode , transformationMode , etc...), just proceed the same way. 如果您需要保存QGraphicsPixmapItem其他属性(例如isActiveisVisibleacceptDropoffsetshapeModetransformationMode等), shapeMode相同的方式进行。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM