{
qint32 margin = 40 ;
QRect rct
= printer.
paperRect().
adjusted(margin, margin,
-margin,
-margin
) ;
painter.drawRect(rct) ;
toption.setTextDirection(Qt::RightToLeft) ;
QFont fontTitle
("Arial",
36) ;
fontTitle.setBold(true);
painter.setFont(fontTitle);
QString title
= ui
->leTitle
->text
().
trimmed() ;
toption.setAlignment(Qt::AlignTop|Qt::AlignHCenter);
painter.drawText(rct.adjusted(0, 10, 0, 0), title, toption);
QFont fontText
("Arial",
28) ;
painter.setFont(fontText) ;
QString text
= ui
->teText
->toPlainText
() ;
toption.setAlignment(Qt::AlignLeft|Qt::AlignHCenter);
painter.drawText(rct.adjusted(0, 10+painter.fontMetrics().height()*4, 0, 0), text, toption);
}
void Widget::on_pbPDF_clicked()
{
if(fileName.isEmpty())
return ;
printer.
setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName(fileName);
printDocument(painter, printer) ;
painter.end() ;
}
void Widget::printDocument(QPainter &painter, QPrinter &printer)
{
qint32 margin = 40 ;
QRect rct = printer.paperRect().adjusted(margin, margin, -margin, -margin) ;
painter.drawRect(rct) ;
QTextOption toption ;
toption.setTextDirection(Qt::RightToLeft) ;
QFont fontTitle("Arial", 36) ;
fontTitle.setBold(true);
painter.setFont(fontTitle);
QString title = ui->leTitle->text().trimmed() ;
toption.setAlignment(Qt::AlignTop|Qt::AlignHCenter);
painter.drawText(rct.adjusted(0, 10, 0, 0), title, toption);
QFont fontText("Arial", 28) ;
painter.setFont(fontText) ;
QString text = ui->teText->toPlainText() ;
toption.setAlignment(Qt::AlignLeft|Qt::AlignHCenter);
painter.drawText(rct.adjusted(0, 10+painter.fontMetrics().height()*4, 0, 0), text, toption);
}
void Widget::on_pbPDF_clicked()
{
QString fileName = QFileDialog::getSaveFileName(this, QString(), QString(), QString("*.pdf(*.pdf)")) ;
if(fileName.isEmpty())
return ;
QPrinter printer ;
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName(fileName);
QPainter painter(&printer) ;
printDocument(painter, printer) ;
painter.end() ;
QDesktopServices::openUrl(QUrl(fileName)) ;
}
To copy to clipboard, switch view to plain text mode
Bookmarks