Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Export QPlainTextEdit to SVG

[Solved] Export QPlainTextEdit to SVG

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 2.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dvillalobos
    wrote on last edited by
    #1

    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));
    @

    Regards

    David

    1 Reply Last reply
    0
    • L Offline
      L Offline
      leon.anavi
      wrote on last edited by
      #2

      "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.

      http://anavi.org/

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dvillalobos
        wrote on last edited by
        #3

        I fund that the file is created, but is empty.

        Regards

        David

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dvillalobos
          wrote on last edited by
          #4

          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.

          Regards

          David

          1 Reply Last reply
          0
          • L Offline
            L Offline
            leon.anavi
            wrote on last edited by
            #5

            [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.

            http://anavi.org/

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dvillalobos
              wrote on last edited by
              #6

              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?

              Regards

              David

              1 Reply Last reply
              0
              • D Offline
                D Offline
                dvillalobos
                wrote on last edited by
                #7

                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);@

                Regards

                David

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved