I have been reading the Qt documentation for QPrinter and QPrintDialog but I cannot figure how to print the contents in a textEdit field.
Here is the code I have been try and which of course doesn't work.
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QtPrintSupport/QPrinter>
#include <QtPrintSupport/QPrintDialog>
#include <QPainter>
void MainWindow::on_pushButton_clicked()
{
QString textFromField = ui->textEdit->toPlainText();
QPrinter printer;
QPrintDialog *printDialog = new QPrintDialog(&printer, this);
printDialog->setWindowTitle("Print Document");
if(printDialog->exec() != QDialog::Accepted)
{
}
QPainter painter;
painter.begin(&printer);
painter.drawText(100, 100, 500, 500,Qt::AlignLeft | Qt::AlignTop, textFromField);
painter.end();
}
This is the output I get when I run it.
:-1: warning: directory not found for option '-F/Applications/Qt5.1.0//5.1.0/clang_64/qtbase/lib' :-1: error: symbol(s) not found for architecture x86_64
Any idea what am I doing wrong? Again all I want is to print the contents in a textEdit field.
The QTextEdit is just an editor for a QTextDocument, which is a powerful class that knows how to print it's content.
QTextDocument *doc = ui->textEdit->document();
doc->print(&printer);
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.