Print 2 textEdit on the same pdf



  • Hello,

    I tried to print 2 textEdit in a row, but i get one or the other, not both. My code can print both "textEdit_7" and a Customplot but when "commentary==1" i don't get the extra "textEdit_9" that i want in my document. Infact when "commentary==1" i only have textEdit_9 on my sheet. I think that i' don't know how to use Qprinter or QtextCursor correctly.
    Can you help me fixing that ?

    Here is my code :

    void FenMain::on_pushButton_3_clicked()
    {
    ui->textEdit_7->setText("");
    QTextCursor cursor = ui->textEdit_7->textCursor();
    QTextCursor cursoor = ui->textEdit_9->textCursor();
    
    // insert the current plot at the cursor position. QCPDocumentObject::generatePlotFormat creates a
    // vectorized snapshot of the passed plot (with the specified width and height) which gets inserted
    // into the text document.
    cursor.insertText("State of last cycle ");
    cursor.insertText(QDateTime::currentDateTime().toS tring("dd/MM/yyyy hh:mm"));
    cursor.insertText(QString(QChar::ObjectReplacement Character), QCPDocumentObject::generatePlotFormat(ui->CustomPlot, 700, 300));
    cursor.insertText("Comment :");
    
    ui->textEdit_7->setTextCursor(cursor);
    ui->textEdit_9->setTextCursor(cursoor);
    QString fileName = QFileDialog::getSaveFileName(this, "Save document...", qApp->applicationDirPath(), "*.pdf");
    
    if (!fileName.isEmpty())
    {
    if (!(fileName.contains(".pdf")) && !(fileName.contains(".PDF")))
    {
    fileName.append(".pdf");
    }
    }
    QPrinter printer;
    printer.setFullPage(true);
    printer.setPaperSize(QPrinter::A4);
    printer.setOrientation(QPrinter::Portrait);
    printer.setOutputFormat(QPrinter::PdfFormat);
    printer.setOutputFileName(fileName);
    ui->textEdit_7->document()->print(&printer);
    if(commentary == 1)
    {
    ui->textEdit_9->document()->print(&printer);
    commentary=0;
    }
    else {}
    QDesktopServices::openUrl(QUrl(fileName));
    }
    

    Thanks in advance
    Best regards,


  • Moderators

    @Ronctis Maybe you should first construct a string containing everything you want to print and then print this string?



  • Thanks for your answer ! I was thinking of doing that.

    I found these information in the documentation and online :

    QString::QString(const QString &other);
    QString text = edit->toPlainText();
    

    Then i tried to use it raw in my code:

    QString textEdit_9 = edit->toPlainText();
    QString::QString(const QString &textEdit_9);
    

    error message:
    -'edit' was not declared in this scope.
    -excepted primary-expression before 'const'
    -cannot call constructor 'QString::QString' directly [-fpermissive]

    Can you give me a general example of how to convert a textEdit content into a string, that i can reuse?

    Best regards,



  • There is no easy, out of the box, way to do this and it has been a long standing "feature request" in Qt. See https://forum.qt.io/topic/69517/merge-qtextdocument

    @Ronctis said in Print 2 textEdit on the same pdf:

    error message:
    -'edit' was not declared in this scope.

    edit is just a placeholder, it should be ui->textEdit_7 or ui->textEdit_9 in your case


  • Moderators

    @Ronctis said in Print 2 textEdit on the same pdf:

    QString::QString(const QString &textEdit_9);

    this is no valid C++.
    What are you trying to do?

    You just need to construct a string:

    QString result = QString("%1 %2").arg(edit->toPlainText()).arg(edit2->toPlainText());
    


  • @jsulm Thank you, it does work. Now i need to figure out how to print it.



  • If all you need is just plain text then it's super easy!

    QTextDocument textDoc;
    QTextCursor textCur(&textDoc);
    textCur.insertText("State of last cycle ");
    textCur.insertText(QDateTime::currentDateTime().toS tring("dd/MM/yyyy hh:mm"));
    textCur.insertText(QString(QChar::ObjectReplacement Character), QCPDocumentObject::generatePlotFormat(ui->CustomPlot, 700, 300));
    textCur.insertText("Comment :");
    textCur.insertText(ui->textEdit_9->toPlainText());
    QString fileName = QFileDialog::getSaveFileName(this, "Save document...", qApp->applicationDirPath(), "*.pdf");
    if (!fileName.isEmpty())
    {
    if (!(fileName.contains(".pdf")) && !(fileName.contains(".PDF")))
    fileName.append(".pdf");
    
    QPrinter printer;
    printer.setFullPage(true);
    printer.setPaperSize(QPrinter::A4);
    printer.setOrientation(QPrinter::Portrait);
    printer.setOutputFormat(QPrinter::PdfFormat);
    printer.setOutputFileName(fileName);
    textDoc.print(&printer);
    }
    


  • Code update :

    void FenMain::on_pushButton_3_clicked()
    {
            ui->textEdit_7->setText("");
    
            QString result = QString("%1 %2").arg(ui->textEdit_7->toPlainText().arg(ui->textEdit_9->toPlainText()));  //**new**
    
            QTextCursor cursor = ui->textEdit_7->textCursor();
    
            QTextDocument textDoc; //**new**
            QTextCursor textCur(&textDoc); //**new**
    
            // insert the current plot at the cursor position. QCPDocumentObject::generatePlotFormat creates a vectorized snapshot of the passed plot (with the specified width and height) which gets inserted into the text document.
            cursor.insertText("State of last cycle ");
            cursor.insertText(QDateTime::currentDateTime().toString("dd/MM/yyyy hh:mm"));
            cursor.insertText(QString(QChar::ObjectReplacementCharacter), QCPDocumentObject::generatePlotFormat(ui->CustomPlot, 700, 300));
            cursor.insertText("Comment :");
    
            textCur.insertText(ui->textEdit_9->toPlainText());//**new**
    
            ui->textEdit_7->setTextCursor(cursor);
    
            QString fileName = QFileDialog::getSaveFileName(this, "Save document...", qApp->applicationDirPath(), "*.pdf");
    
            if (!fileName.isEmpty())
                {
                    if (!(fileName.contains(".pdf")) && !(fileName.contains(".PDF")))
                        {
                            fileName.append(".pdf");
                        }
                }
            QPrinter printer;
            printer.setFullPage(true);
            printer.setPaperSize(QPrinter::A4);
            printer.setOrientation(QPrinter::Portrait);
            printer.setOutputFormat(QPrinter::PdfFormat);
            printer.setOutputFileName(fileName);
            ui->textEdit_7->document()->print(&printer);
    
            if(commentary == 1)
            {
                textDoc.print(&printer); //**new**
                commentary=0;
            }
            else {}
            QDesktopServices::openUrl(QUrl(fileName));
    }
    

    Still, when commentary==1 it prints only the "textEdit_9" content via the line "textDoc.print(&printer);".
    I use "&printer" in my code both for the textEdit_7 and textEdit9, so i don't get both in my pdf sheet. (It might be the problem)

    I tried @VRonin code it works partially : it displays "State of the cycle", the date, textEdit_9 content, but not my plot which is related to textEdit_7 and need it to be built properly.

    I added this line according to @jsulm : "QString result = QString("%1 %2").arg(ui->textEdit_7->toPlainText().arg(ui->textEdit_9->toPlainText()));", which is not used after. I would like to print "result" which should contain both textEdit_7 and textEdit_9. But "result" belongs to QString and not QPrinter, so i can't print it.

    Can i do some tweeks to print "result" ?


  • Moderators

    @Ronctis said in Print 2 textEdit on the same pdf:

    QString result = QString("%1 %2").arg(ui->textEdit_7->toPlainText().arg(ui->textEdit_9->toPlainText()));

    This is wrong. It must be

    QString result = QString("%1 %2").arg(ui->textEdit_7->toPlainText()).arg(ui->textEdit_9->toPlainText()));
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.