简体   繁体   中英

Print a textEdit in Qt

How can I print the text available in a textEdit using Qt creator (C++)? Please help me with this. I created a note pad using a textEdit . Now I want to print the note pad content. That mean the text typed in textEdit . So please help me.

please mention header files that I need to include as well.

Here is something I tried previous. but it's not working. so please help me with this.

void MainWindow::on_action_Print_triggered()
{
    QString textFromField = ui->txtEdit->toPlainText();

    QPrinter printer(QPrinter::HighResolution);
        printer.setOutputFileName("print.ps");
        QPainter painter;
        painter.begin(&printer);

                printer.newPage();

        painter.end();
}

QTextEdit already has method which allows you print it's content, so you don't need QPainter . Use this (I printed pdf as example):

QPrinter printer(QPrinter::HighResolution);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName("outputt.pdf");
ui->textEdit->print(&printer);

print()

And of course you need

#include <QPrinter>

but I think that it is already added in your project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM