PDA

View Full Version : SVG to QByteArray



meazza
4th July 2011, 17:09
Hello dear programmers.

Have searched everywhere on the net for this information but could not find something that helped me solve my problem.

I want to convert an SVG file to an QByteArray. This I want to do because I want to save that SVG image in QByteArray::toBase64() format. And since there in QSvgRenderer constructor there is a possibility to load from a QByteArray I would think this is possible but me not being able to solve this.

Appreciate all the help I can get.

mvuori
4th July 2011, 18:43
The way I see it, forget that the file is an SVG file; don't think of the problem as conversion, but just as reading a file into a QByteArray. Just create a QBytearray and read the file into it, not caring what the contents are.

ChrisW67
5th July 2011, 04:09
I am not sure why you would want to Base64 encode an SVG file: it is already text after all.


QFile input("myfile.svg");
QFile output("myfile.b64");
if (input.open(QIODevice::ReadOnly) && output.open(QIODevice::WriteOnly)) {
QByteArray ba = input.readAll();
input.close();
output.write(ba.toBase64());
output.close();
}

meazza
5th July 2011, 08:56
I am not sure why you would want to Base64 encode an SVG file: it is already text after all.

Well I have an application where you can load different types of images (svg, pbg and jpg) and I am saving PNG and JPG base64 encoded to an xml document so thought might aswell do that to the SVG images aswell.

Am I losing something when I do this or?

ChrisW67
6th July 2011, 00:05
If you are doing this to store an arbitrary binary blob inside another text format then this is a reasonable way to do it. You should not be losing anything except a little bit of disk space for the large Base64 version.