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. Writing .doc in Qt
Qt 6.11 is out! See what's new in the release blog

Writing .doc in Qt

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 2.9k Views 2 Watching
  • 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.
  • ivanicyI Offline
    ivanicyI Offline
    ivanicy
    wrote on last edited by
    #1

    Hello.

    Someone knows how to export the QPrinter contents in .doc?

    I could do it in pdf but, I can't do in .doc. Any idea?

    Thank you very much

    EddyE 1 Reply Last reply
    0
    • JohanSoloJ Offline
      JohanSoloJ Offline
      JohanSolo
      wrote on last edited by
      #2

      I doubt you can "print" to doc...

      `They did not know it was impossible, so they did it.'
      -- Mark Twain

      1 Reply Last reply
      0
      • ivanicyI ivanicy

        Hello.

        Someone knows how to export the QPrinter contents in .doc?

        I could do it in pdf but, I can't do in .doc. Any idea?

        Thank you very much

        EddyE Offline
        EddyE Offline
        Eddy
        wrote on last edited by
        #3

        @ivanicy
        Could you elaborate on what you want to do exactly?

        Maybe you just want to write to a Microsoft Word doc file format?

        Qt Certified Specialist
        www.edalsolutions.be

        1 Reply Last reply
        0
        • ivanicyI Offline
          ivanicyI Offline
          ivanicy
          wrote on last edited by
          #4

          Yes sorry. This code generates a pdf document. I want to do something similar but in .doc to open with Microsoft Word:

          if (pdfButtonClicked) {
                  QIcon icon(":/MainIcons/Resources/pdf_pressed_2.png");
                  icon.addPixmap(QPixmap(":/MainIcons/Resources/pdf_pressed_2.png"));
                  ui->pdfButton->setIcon(icon);
          
                  printer.setPageSize(QPrinter::A4);
                  printer.setOrientation(QPrinter::Portrait);
                  printer.setOutputFormat(QPrinter::PdfFormat);
          
                  QString fileName = QFileDialog::getSaveFileName(this, "Save pdf...", QCoreApplication::applicationDirPath(), "PDF (*.pdf)" );
                  if (!fileName.isNull())
                  {
                      printer.setOutputFileName(fileName);
                      QImage imagen = screenshot.toImage();
                      imagen = imagen.scaled(imagen.width() - 300, imagen.height()-100);
                      QPainter painter(&printer);
                      QRect rect = painter.viewport();
                      rect.setSize(QSize(imagen.width(), imagen.height()));
                      QSize size = imagen.size();
                      size.scale(rect.size(), Qt::KeepAspectRatio);
                      double xscale = printer.pageRect().width() / double(imagen.width()*1.19);
                      double yscale = printer.pageRect().height() / double(imagen.height());
          
                      double scale = qMin(xscale, yscale);
                      painter.scale(scale, scale);
                      painter.translate(150, 200);
                      painter.drawImage(0, 0, imagen);
                      painter.end();
          
                      QIcon icon(":/MainIcons/Resources/pdf.png");
                      icon.addPixmap(QPixmap(":/MainIcons/Resources/pdf.png"));
                      ui->pdfButton->setIcon(icon);
                      pdfButtonClicked = false;
                      QMessageBox::information(NULL, "Export PDF", "PDF created.", QMessageBox::Ok);
                  } else {
                      QIcon icon(":/MainIcons/Resources/pdf.png");
                      icon.addPixmap(QPixmap(":/MainIcons/Resources/pdf.png"));
                      ui->pdfButton->setIcon(icon);
                      pdfButtonClicked = false;
                  }
              }
          

          Thank you very much

          1 Reply Last reply
          0
          • EddyE Offline
            EddyE Offline
            Eddy
            wrote on last edited by
            #5

            @ivanicy
            Where do you get the data you want to write in the doc file? What is your starting point?

            Qt Certified Specialist
            www.edalsolutions.be

            ivanicyI 1 Reply Last reply
            0
            • EddyE Eddy

              @ivanicy
              Where do you get the data you want to write in the doc file? What is your starting point?

              ivanicyI Offline
              ivanicyI Offline
              ivanicy
              wrote on last edited by
              #6

              @Eddy In this moment, the "screenshot" is a QImage which I want to show in .doc file. Later, maybe I will want to add some text under the image. I put the image in the pdf document with "painter.drawImage()" function.

              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by VRonin
                #7

                The closest thing is using QTextDocumentWriter

                QTextDocument tempDocument;
                QTextCursor tempCursor(&tempDocument);
                tempCursor.insertImage(imagen);
                // Use tempCursor to add text if you want
                QTextDocumentWriter writer;
                writer.setFormat("odf");
                writer.setFileName(fileName);
                if(!writer.write(&tempDocument))
                qCritical("Save Failed!");
                

                This format is recognised by Libre Office, Open Office and (recent versions of) MS Word

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                ivanicyI 1 Reply Last reply
                2
                • VRoninV VRonin

                  The closest thing is using QTextDocumentWriter

                  QTextDocument tempDocument;
                  QTextCursor tempCursor(&tempDocument);
                  tempCursor.insertImage(imagen);
                  // Use tempCursor to add text if you want
                  QTextDocumentWriter writer;
                  writer.setFormat("odf");
                  writer.setFileName(fileName);
                  if(!writer.write(&tempDocument))
                  qCritical("Save Failed!");
                  

                  This format is recognised by Libre Office, Open Office and (recent versions of) MS Word

                  ivanicyI Offline
                  ivanicyI Offline
                  ivanicy
                  wrote on last edited by
                  #8

                  @VRonin Thank you very much. I made a few changes and it worked perfect!

                  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