QT5.15.2加载pdf为QGraphicsScene的背景

5.15.2使用pdf

必须要安装QT源码,可以看到编译器lib目录已经有pdf相关的lib文件,d是debug

1.找到源码目录:D:\soft\QT\5.15.2\Src\qtwebengine\include

复制这两个文件夹到编译器的包含目录中:D:\soft\QT\5.15.2\msvc2019_64\include

2.找到D:\soft\QT\5.15.2\Src\qtwebengine\src\pdf\api目录,将目录下的文件夹全部复制替换到D:\soft\QT\5.15.2\msvc2019_64\include\QtPdf中。

3.将D:\soft\QT\5.15.2\Src\qtwebengine\src\pdfwidgets以下文件复制替换到到D:\soft\QT\5.15.2\msvc2019_64\include\QtPdfWidgets中

4.修改pro文件,不用加入附加包含目录,只需要加入附加库目录和lib名称即可

cpp 复制代码
LIBS += -LD:/soft/Qt/5.15.2/msvc2019_64/lib

CONFIG(debug, debug|release){
    LIBS += -lQt5Pdfd
    LIBS += -lQt5PdfWidgetsd
}
else
{
    LIBS += -lQt5Pdf
    LIBS += -lQt5PdfWidgets
}

5.测试

cpp 复制代码
#include <QApplication>
#include <QtPdf/QPdfDocument>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QPdfDocument *doc = new QPdfDocument;
    doc->load("D:/1.pdf");
    QSizeF pageSize = doc->pageSize(0);
    QImage image = doc->render(0,pageSize.toSize());
    image.save("D:/1.png");
    return a.exec();
}

加载pdf为QGraphicsScene的背景

cpp 复制代码
#include <QApplication>
#include <QtPdf/QPdfDocument>
#include <QGraphicsScene>
#include <QGraphicsView>
class MyQGraphicsScene:public QGraphicsScene
{
public:
    MyQGraphicsScene(QObject *parent = nullptr):QGraphicsScene(parent)
    {
        doc = new QPdfDocument(this);
    }
    void drawBackground(QPainter *painter, const QRectF &rect) override
    {
        doc->load("D:/1.pdf");
        QSizeF pageSize = doc->pageSize(0);
        QImage image = doc->render(0,pageSize.toSize());
        painter->setPen(Qt::blue);
        painter->setBrush(image);
        painter->drawRect(sceneRect());
    }

private:
    QPdfDocument *doc = nullptr;
};

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

    // 创建自定义场景
    MyQGraphicsScene *scene = new MyQGraphicsScene();
    scene->setSceneRect(0,0,500,400);
    // 创建视图,并设置场景
    QGraphicsView *view = new QGraphicsView(scene);
    view->setFixedSize(800,600);
    view->show();
    return a.exec();
}

重写QGraphicsScene的drawBackground方法即可达到目的

相关推荐
geovindu2 小时前
vue3: pdf.js 3.4.120 using javascript
开发语言·javascript·vue.js·pdf
yaso_zhang4 小时前
当生产了~/qt-arm/bin/qmake,可以单独编译其他-源码的某个模块,如下,编译/qtmultimedia
qt
code bean4 小时前
【Qt/C++】深入理解 Lambda 表达式与 `mutable` 关键字的使用
开发语言·c++·qt
TextIn智能文档云平台13 小时前
PDF文档解析新突破:图表识别、公式还原、手写字体处理,让AI真正读懂复杂文档!
图像处理·人工智能·算法·自然语言处理·pdf·ocr
old_power13 小时前
【Python】PDF文件处理(PyPDF2、borb、fitz)
python·pdf
爱看书的小沐18 小时前
【小沐学GIS】基于C++绘制二维瓦片地图2D Map(QT、OpenGL、GIS)
c++·qt·gis·opengl·glfw·glut·二维地图
炬火初现1 天前
Qt 的原理及使用(1)——qt的背景及安装
开发语言·qt
weixin_1101 天前
Qt 无边框窗口,支持贴边分屏
c++·qt
gaoenyang7605251 天前
QT Creator配置Kit
开发语言·qt
belldeep1 天前
vite:npm 安装 pdfjs-dist , PDF.js View 预览功能示例
javascript·pdf·pdfjs-dist·pdf.worker