Using QSvgGenerator with WebAssembly
-
wrote on 30 Nov 2024, 22:21 last edited by Gilboonet
Hello, my application goal is to generate a svg file. For my first version, I was unable to use QSvgGenerator with WebAssembly simply because I didn't find how to do the following :
To write an SVG file, you first need to configure the output by setting the fileName or outputDevice properties. It is usually necessary to specify the size of the drawing by setting the size property, and in some cases where the drawing will be included in another, the viewBox property also needs to be set.
I found a workaround that worked very well using an external library, but I'm hoping that there is a way to use QSvgGenerator and configure the output that works with WebAssembly.
-
Hello, my application goal is to generate a svg file. For my first version, I was unable to use QSvgGenerator with WebAssembly simply because I didn't find how to do the following :
To write an SVG file, you first need to configure the output by setting the fileName or outputDevice properties. It is usually necessary to specify the size of the drawing by setting the size property, and in some cases where the drawing will be included in another, the viewBox property also needs to be set.
I found a workaround that worked very well using an external library, but I'm hoping that there is a way to use QSvgGenerator and configure the output that works with WebAssembly.
@Gilboonet What is the problem?
https://doc.qt.io/qt-6/qsvggenerator.html even shows some code. -
@Gilboonet What is the problem?
https://doc.qt.io/qt-6/qsvggenerator.html even shows some code.wrote on 2 Dec 2024, 07:43 last edited by@jsulm For the basic usage of QSvgGenerator, there's no problem, on Desktop it works fine, but I didn't find how to make it output using WebAssembly as there are restrictions. I made a post explaining my problem here (more than 1 year ago, so I'm hoping that now someone could help even if it seems that not many people uses WebAssembly) here
-
@jsulm For the basic usage of QSvgGenerator, there's no problem, on Desktop it works fine, but I didn't find how to make it output using WebAssembly as there are restrictions. I made a post explaining my problem here (more than 1 year ago, so I'm hoping that now someone could help even if it seems that not many people uses WebAssembly) here
@Gilboonet I'm still not sure what exactly the problem is.
You can't write in a file?
https://doc.qt.io/qt-6/wasm.html -
@Gilboonet I'm still not sure what exactly the problem is.
You can't write in a file?
https://doc.qt.io/qt-6/wasm.htmlwrote on 2 Dec 2024, 08:00 last edited by@jsulm I'm using WebAssembly since the beginning of this project, about 2 years, and there are still lots of topics where there is not enough information. Here, the problem is that the same code that works on Desktop and saves the file, does nothing and remain silent on WebAssembly. It might be the mechanism that is intended to fire the writing process of the SVG file, with
painter.end;
that should be started using another way, but I still didn't find it.
-
wrote on 2 Dec 2024, 08:13 last edited by Gilboonet 12 Feb 2024, 08:14
With WebAssembly, there's a dedicated function to use with QFileDialog so that you can write a file, I use that function successfully to save my project data file, but what exactly I didn't manage to do is have the svg content of QSvgGenerator transferred to a QByteArray that the function need to save it. For the moment I'm doing this all by an external class, just like I'm doing for the 3d part.
-
With WebAssembly, there's a dedicated function to use with QFileDialog so that you can write a file, I use that function successfully to save my project data file, but what exactly I didn't manage to do is have the svg content of QSvgGenerator transferred to a QByteArray that the function need to save it. For the moment I'm doing this all by an external class, just like I'm doing for the 3d part.
@Gilboonet You can set a https://doc.qt.io/qt-6/qbuffer.html via https://doc.qt.io/qt-6/qsvggenerator.html#outputDevice-prop and after painting call https://doc.qt.io/qt-6/qbuffer.html#data to get the QByteArray.
-
@Gilboonet You can set a https://doc.qt.io/qt-6/qbuffer.html via https://doc.qt.io/qt-6/qsvggenerator.html#outputDevice-prop and after painting call https://doc.qt.io/qt-6/qbuffer.html#data to get the QByteArray.
wrote on 2 Dec 2024, 08:33 last edited by Gilboonet 12 Feb 2024, 10:45@jsulm Thank you, I try that ASAP.
It works fine either on Desktop and Wasm. Now I need to plug my data.
void MainWindow::exporter() { QSvgGenerator SG; SG.setSize(QSize(200, 200)); SG.setViewBox(QRect(0, 0, 200, 200)); SG.setTitle(tr("SVG Generator Example Drawing")); SG.setDescription(tr("An SVG drawing created by the SVG Generator " "Example provided with Qt.")); QPainter painter; QBuffer buffer; SG.setOutputDevice(&buffer); painter.begin(&SG); painter.end(); QFileDialog::saveFileContent(buffer.data(), "myExport.svg"); }
-
-
8/8