[Solved] Export QPlainTextEdit to SVG
-
Hi all,
I have an slot to export the context of a class of type QPlainTextEdit to SVG, here is my code, but it does not work, any ideas?
@
QApplication::setOverrideCursor(Qt::WaitCursor);
QFileDialog fileDialog;
fileDialog.setDirectory(QDir::home());
QString file = fileDialog.getSaveFileName(this, tr("Save to Svg"), settings.value("LastFileSvg", QDir::home().absolutePath()).toString(), tr("Svg files (*.svg)"));
settings.setValue("LastFileSvg", fileDialog.directory().filePath(file));
QSvgGenerator svg;
svg.setFileName(file);
QRect rect = printer.pageRect().adjusted(25, 25, -25, 25);
svg.setSize(rect.size());
QPainter painter(&svg);
painter.setViewport(rect);
document()->drawContents(&painter);
QApplication::restoreOverrideCursor();
parentWidget->statusBarMessage(tr("File saved at: %1").arg(file));
@ -
"SVG Generator Example":http://doc-snapshot.qt-project.org/4.8/painting-svggenerator.html is a very good and simple application that demonstrates how to use QSvgGenerator and might be useful in your case.
-
I fund that the file is created, but is empty.
-
Hi leon.anavi
Thank for your reply.
Yes, it was very useful. But I have already take a look there and you'll see the code is quite similar.
-
[quote author="David Villalobos" date="1353968980"]Yes, it was very useful. But I have already take a look there and you'll see the code is quite similar.[/quote]
Yes, it is similar but there are also some differences that might be important. Try to set the size and the viewBox of the QSvgGenerator object.
-
The QSvgGenerator is working, if add this line the rectangle is drawn, I think the problem is in document()->drawContents(&painter)
@
painter.fillRect(QRect(0, 0, 200, 200), Qt::darkGreen);
@Any idea?
-
Here is the code:
@ QFileDialog fileDialog;
fileDialog.setDirectory(QDir::home());
QString file = fileDialog.getSaveFileName(this, tr("Save to Svg"), settings.value("LastFileSvg", QDir::home().absolutePath()).toString(), tr("Svg files (*.svg)"));
settings.setValue("LastFileSvg", fileDialog.directory().filePath(file));
QSvgGenerator svg;
svg.setFileName(file);
QTextDocument *doc = document()->clone();
QRect rectSize(0, 0, (int) doc->size().width(), (int) doc->size().height());
QPainter painter(&svg);
painter.setViewport(rectSize);
doc->documentLayout()->setPaintDevice(&svg);
doc->drawContents(&painter);@