如何仅使用最少的内容将小部件打印为 PDF?
How to print a widget to PDF only with the minimal content?
我使用以下代码打印一个小部件(来自 QTabWidget 的选项卡)及其内容:
void MainWindow::print()
{
quint8 tabIndex = quint8(ui->tabWidget->currentIndex());
CharacterSheetWidget* widget = dynamic_cast< CharacterSheetWidget* >( ui->tabWidget->widget(tabIndex) );
QString filePath = QFileDialog::getSaveFileName(this, QString(tr("Print as PDF...")), QString(), "PDF file (*.pdf)");
if(filePath.isEmpty())
return;
QPrinter printer;
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName(filePath);
printer.setPaperSize(QPrinter::A4);
printer.setResolution(300);
printer.setFullPage(false);
printer.setPageMargins(5, 5, 5, 5, QPrinter::Millimeter);
QString originalStyle = widget->styleSheet();
widget->setStyleSheet("background-color:white;");
QPainter painter;
painter.begin(&printer);
double xscale = printer.pageRect().width() / double(widget->width());
double yscale = printer.pageRect().height() / double(widget->height());
double scale = qMin(xscale, yscale);
painter.translate(printer.paperRect().center());
painter.scale(scale, scale);
painter.translate(- widget->width()/ 2, - widget->height()/ 2);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
widget->render(&painter);
painter.end();
widget->setStyleSheet(originalStyle);
}
但它在右侧和底部打印了很多 space。
我想用最小的内容区域打印它。
例如在这张图片上,您看到它打印了 pdf 中的红色区域,我希望它打印蓝色区域。
我该怎么办(如果您需要更多信息,请告诉我)?
顺便说一下,我创建了一个解决方案:
在 "QPainter painter;" 之前,我添加了这段代码:
QSize minSize = widget->minimumSizeHint();
int minWidth = minSize.width();
int minHeight = minSize.height();
并将所有 widget->width() 和 widget->height 替换为 minWidth 和 minHeight。
我使用以下代码打印一个小部件(来自 QTabWidget 的选项卡)及其内容:
void MainWindow::print()
{
quint8 tabIndex = quint8(ui->tabWidget->currentIndex());
CharacterSheetWidget* widget = dynamic_cast< CharacterSheetWidget* >( ui->tabWidget->widget(tabIndex) );
QString filePath = QFileDialog::getSaveFileName(this, QString(tr("Print as PDF...")), QString(), "PDF file (*.pdf)");
if(filePath.isEmpty())
return;
QPrinter printer;
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName(filePath);
printer.setPaperSize(QPrinter::A4);
printer.setResolution(300);
printer.setFullPage(false);
printer.setPageMargins(5, 5, 5, 5, QPrinter::Millimeter);
QString originalStyle = widget->styleSheet();
widget->setStyleSheet("background-color:white;");
QPainter painter;
painter.begin(&printer);
double xscale = printer.pageRect().width() / double(widget->width());
double yscale = printer.pageRect().height() / double(widget->height());
double scale = qMin(xscale, yscale);
painter.translate(printer.paperRect().center());
painter.scale(scale, scale);
painter.translate(- widget->width()/ 2, - widget->height()/ 2);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
widget->render(&painter);
painter.end();
widget->setStyleSheet(originalStyle);
}
但它在右侧和底部打印了很多 space。 我想用最小的内容区域打印它。
例如在这张图片上,您看到它打印了 pdf 中的红色区域,我希望它打印蓝色区域。
我该怎么办(如果您需要更多信息,请告诉我)?
顺便说一下,我创建了一个解决方案: 在 "QPainter painter;" 之前,我添加了这段代码:
QSize minSize = widget->minimumSizeHint();
int minWidth = minSize.width();
int minHeight = minSize.height();
并将所有 widget->width() 和 widget->height 替换为 minWidth 和 minHeight。