c++ 如何在Qt中打印QWidget?

svdrlsy4  于 2023-07-01  发布在  其他
关注(0)|答案(2)|浏览(202)

我需要知道如何将QWidget打印为PDF文件。Widget(QDialog)包含许多标签、一些QPlainTextEdit和一个背景图像。对话框显示一个已填写所有字段的收据。
我已经尝试使用QTextDocument和html来实现这个目的,但是收据的复杂性(大量的图像和格式定制)使得html输出完全混乱。
这是文件。
收据图像

wi3ka0sx

wi3ka0sx1#

你必须使用QPrinter,这是你必须使用的对象,需要QPainterQPrinter中绘制小部件。

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QDialog w;

    w.setLayout(new QVBoxLayout());
    w.layout()->addWidget(new QLineEdit("text"));
    w.layout()->addWidget(new QPushButton("btn"));
    w.layout()->addWidget(new QPlainTextEdit("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris rutrum magna semper nisi faucibus, at auctor dolor ullamcorper. Phasellus facilisis blandit augue sit amet placerat. Aliquam nec imperdiet diam. Proin dignissim vulputate metus, nec tincidunt magna vulputate ac. Praesent vel felis ac dolor viverra tempus eu vitae neque. Nulla efficitur gravida arcu id suscipit. Maecenas placerat egestas velit quis interdum. Nulla diam massa, hendrerit vitae mi et, placerat aliquam nisl. Donec tincidunt lobortis orci, quis egestas augue tempus sed. Nulla vel dolor eget ipsum accumsan placerat ut at magna."));
    w.show();

    QPushButton btn("print");
    btn.show();

    QObject::connect(&btn, &QPushButton::clicked, [&w](){
        QPrinter printer(QPrinter::HighResolution);
        printer.setOutputFormat(QPrinter::PdfFormat);
        printer.setOutputFileName("output.pdf");
        printer.setPageMargins(12, 16, 12, 20, QPrinter::Millimeter);
        printer.setFullPage(false);

        QPainter painter(&printer);

        double xscale = printer.pageRect().width() / double(w.width());
        double yscale = printer.pageRect().height() / double(w.height());
        double scale = qMin(xscale, yscale);
        painter.translate(printer.paperRect().center());
        painter.scale(scale, scale);
        painter.translate(-w.width()/ 2, -w.height()/ 2);
        w.render(&painter);
    });

    return a.exec();
}

小部件:

output.pdf

yshpjwxd

yshpjwxd2#

Eyllanesc的解决方案工作得很好,但我在pageRect()paperRect()函数上有警告。这个功能似乎已经过时了。下面是不包含弃用函数的before和after代码。

//-------------------------- Code BEFORE --------------------------//
double xscale = printer.pageRect().width() / double(w.width());
double yscale = printer.pageRect().height() / double(w.height());
double scale = qMin(xscale, yscale);
painter.translate(printer.paperRect().center());
painter.scale(scale, scale);
painter.translate(-w.width()/ 2, -w.height()/ 2);
w.render(&painter);
//-----------------------------------------------------------------//

//-------------------------- Code AFTER ---------------------------//
double pageRectWidth  = printer.pageLayout().pageSize().sizePixels(96).width()-printer.pageLayout().marginsPixels(96).left()-printer.pageLayout().marginsPixels(96).right();
double pageRectHeight = printer.pageLayout().pageSize().sizePixels(96).height()-printer.pageLayout().marginsPixels(96).top()-printer.pageLayout().marginsPixels(96).bottom();

double xscale = pageRectWidth / double(w.width());
double yscale = pageRectHeight / double(w.height());
double scale = qMin(xscale, yscale);
painter.translate(QPoint(printer.pageLayout().pageSize().sizePixels(96).width()/2,printer.pageLayout().pageSize().sizePixels(96).height()/2));
painter.scale(scale, scale);
painter.translate(-w.width()/ 2, -w.height()/ 2);
w.render(&painter);
//-----------------------------------------------------------------//

我在sizePixels()函数中使用了96,因为它是默认分辨率。
希望这段代码能帮上忙:)

相关问题