导出为 PDF UI 场景 (QML)
Export as PDF UI Scene(QML)
我希望将界面中出现的图像和文本呈现为PDF。我有一个按钮,当按钮被触发时,它会将屏幕上显示在界面中的任何内容保存为 PDF(如导出为 PDF)。为此,我查看了论坛中打开的资源和主题,但没有得出明确的结果。如果你能帮上忙,我会很高兴。
您应该在 .pro
文件中启用 PrintSupport
模块,例如 QT+=PrintSupport
首先像这样声明 Drawer class :
#pragma once
#include <QQmlApplicationEngine>
class PdfExporter : public QQmlApplicationEngine
{
Q_OBJECT
public:
PdfExporter(QQmlApplicationEngine * engine);
Q_INVOKABLE void screenShot();
private:
QQmlApplicationEngine * mEngine;
};
并定义:
#include <Drawer.h>
#include <QQuickWindow>
#include <QPrinter>
#include <QPainter>
PdfExporter::PdfExporter(QQmlApplicationEngine *engine) : QQmlApplicationEngine(engine), mEngine(engine)
{
}
void PdfExporter::screenShot()
{
foreach(QObject* obj, mEngine->rootObjects()) {
QQuickWindow* window = qobject_cast<QQuickWindow*>(obj);
if (window)
{
QImage windowImage = window->grabWindow();
QPrinter pdfPrinter(QPrinter::HighResolution);
pdfPrinter.setOutputFormat(QPrinter::PdfFormat);
pdfPrinter.setOutputFileName("test.pdf");
QPainter painter;
painter.begin(&pdfPrinter);
painter.drawImage(QRect(0,0,windowImage.width(),windowImage.height()),windowImage,{0,0,windowImage.width(),windowImage.height()});
painter.end();
}
}
}
在 main.cpp
中,我更喜欢将 C++ class 作为上下文 属性 导出到引擎。
PdfExporter * dr = new PdfExporter(&engine);
engine.rootContext()->setContextProperty("drawer",dr);
在 QML 中你可以触发像这样的函数:
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
ColumnLayout
{
anchors.centerIn: parent
Rectangle
{
Layout.preferredWidth: 200
Layout.preferredHeight: 100
color: "red"
}
Button
{
Layout.preferredWidth: 100
Layout.preferredHeight: 20
Layout.alignment: Qt.AlignHCenter
text: "test"
onClicked: drawer.screenShot()
}
}
}
我希望将界面中出现的图像和文本呈现为PDF。我有一个按钮,当按钮被触发时,它会将屏幕上显示在界面中的任何内容保存为 PDF(如导出为 PDF)。为此,我查看了论坛中打开的资源和主题,但没有得出明确的结果。如果你能帮上忙,我会很高兴。
您应该在 .pro
文件中启用 PrintSupport
模块,例如 QT+=PrintSupport
首先像这样声明 Drawer class :
#pragma once
#include <QQmlApplicationEngine>
class PdfExporter : public QQmlApplicationEngine
{
Q_OBJECT
public:
PdfExporter(QQmlApplicationEngine * engine);
Q_INVOKABLE void screenShot();
private:
QQmlApplicationEngine * mEngine;
};
并定义:
#include <Drawer.h>
#include <QQuickWindow>
#include <QPrinter>
#include <QPainter>
PdfExporter::PdfExporter(QQmlApplicationEngine *engine) : QQmlApplicationEngine(engine), mEngine(engine)
{
}
void PdfExporter::screenShot()
{
foreach(QObject* obj, mEngine->rootObjects()) {
QQuickWindow* window = qobject_cast<QQuickWindow*>(obj);
if (window)
{
QImage windowImage = window->grabWindow();
QPrinter pdfPrinter(QPrinter::HighResolution);
pdfPrinter.setOutputFormat(QPrinter::PdfFormat);
pdfPrinter.setOutputFileName("test.pdf");
QPainter painter;
painter.begin(&pdfPrinter);
painter.drawImage(QRect(0,0,windowImage.width(),windowImage.height()),windowImage,{0,0,windowImage.width(),windowImage.height()});
painter.end();
}
}
}
在 main.cpp
中,我更喜欢将 C++ class 作为上下文 属性 导出到引擎。
PdfExporter * dr = new PdfExporter(&engine);
engine.rootContext()->setContextProperty("drawer",dr);
在 QML 中你可以触发像这样的函数:
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
ColumnLayout
{
anchors.centerIn: parent
Rectangle
{
Layout.preferredWidth: 200
Layout.preferredHeight: 100
color: "red"
}
Button
{
Layout.preferredWidth: 100
Layout.preferredHeight: 20
Layout.alignment: Qt.AlignHCenter
text: "test"
onClicked: drawer.screenShot()
}
}
}