很高兴在雪易的CSDN遇见你 ,给你糖糖
欢迎大家加入雪易社区-CSDN社区云
前言
本文分享QT开发PDF阅读器技术,希望对各位小伙伴有所帮助!
感谢各位小伙伴的点赞+关注,小易会继续努力分享,一起进步!
你的点赞就是我的动力(^U^)ノ~YO
结果展示:
我将收获到的:
1. 如何创建PDFViewer
2.创建PDFViewer所依赖的QT库
3.PDFViewer的用处
目录
前言
1.准备PDFViewer所依赖的库
2.创建PDF Viewer
小结:
1.准备PDFViewer所依赖的库
》使用开发的QT版本:5.15.2
》存在问题:不能直接添加PDF模块
》原因:QT5.15.2版本已带有pdf模块,但并未在include中包含,但是在lib和bin目录下能找到相应的.lib和.dll文件。
》解决方案:
》下载QT5.15.2源码
》将“Qt\5.15.2\Src\qtwebengine\include”文件夹下的QtPdf和QtPdfWidgets复制到“Qt\5.15.2\msvc2019_64\include”文件夹下。
》 将“Qt\5.15.2\Src\qtwebengine\src\pdf\api”文件夹下的.h文件复制到“Qt\5.15.2\msvc2019_64\include\QtPdf”文件夹下。
》 将“Qt\5.15.2\Src\qtwebengine\src\pdfwidgets”文件夹下的.h文件复制到“Qt\5.15.2\msvc2019_64\include\QtPdfWidgets”文件夹下
》我已将QtPdf整理成单独的依赖库,需要的小伙伴可以联系我下载。
也上传CSDN,下载地址:QT开发PDF阅读器,代码简洁易用!资源-CSDN文库
2.创建PDF Viewer
》创建UI文件
》核心为PageSelector(页面选择)和ZoomSelector(缩放)
》下载地址:QT开发PDF阅读器,代码简洁易用!资源-CSDN文库
》代码如下:
PageSelector.h文件
#ifndef PAGESELECTOR_H
#define PAGESELECTOR_H
#include <QWidget>
class QLabel;
class QLineEdit;
class QPdfDocument;
class QPdfPageNavigation;
class QToolButton;
class PageSelector : public QWidget
{
Q_OBJECT
public:
explicit PageSelector(QWidget *parent = nullptr);
void setPageNavigation(QPdfPageNavigation *pageNavigation);
private slots:
void onCurrentPageChanged(int page);
void pageNumberEdited();
private:
QPdfPageNavigation *m_pageNavigation;
QLineEdit *m_pageNumberEdit;
QLabel *m_pageCountLabel;
QToolButton *m_previousPageButton;
QToolButton *m_nextPageButton;
};
#endif // PAGESELECTOR_H
PageSelector.cpp文件
#include "pageselector.h"
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPdfPageNavigation>
#include <QToolButton>
PageSelector::PageSelector(QWidget *parent)
: QWidget(parent)
, m_pageNavigation(nullptr)
{
QHBoxLayout *layout = new QHBoxLayout(this);
m_previousPageButton = new QToolButton(this);
m_previousPageButton->setText("<");
m_previousPageButton->setEnabled(false);
m_pageNumberEdit = new QLineEdit(this);
m_pageNumberEdit->setAlignment(Qt::AlignRight);
m_pageCountLabel = new QLabel(this);
m_pageCountLabel->setText("0");
m_nextPageButton = new QToolButton(this);
m_nextPageButton->setText(">");
m_nextPageButton->setEnabled(false);
layout->addWidget(m_previousPageButton);
layout->addWidget(m_pageNumberEdit);
layout->addWidget(m_pageCountLabel);
layout->addWidget(m_nextPageButton);
}
void PageSelector::setPageNavigation(QPdfPageNavigation *pageNavigation)
{
m_pageNavigation = pageNavigation;
connect(m_previousPageButton, &QToolButton::clicked, m_pageNavigation, &QPdfPageNavigation::goToPreviousPage);
connect(m_pageNavigation, &QPdfPageNavigation::canGoToPreviousPageChanged, m_previousPageButton, &QToolButton::setEnabled);
connect(m_pageNavigation, &QPdfPageNavigation::currentPageChanged, this, &PageSelector::onCurrentPageChanged);
connect(m_pageNavigation, &QPdfPageNavigation::pageCountChanged, this, [this](int pageCount){ m_pageCountLabel->setText(QString::fromLatin1("/ %1").arg(pageCount)); });
connect(m_pageNumberEdit, &QLineEdit::editingFinished, this, &PageSelector::pageNumberEdited);
connect(m_nextPageButton, &QToolButton::clicked, m_pageNavigation, &QPdfPageNavigation::goToNextPage);
connect(m_pageNavigation, &QPdfPageNavigation::canGoToNextPageChanged, m_nextPageButton, &QToolButton::setEnabled);
onCurrentPageChanged(m_pageNavigation->currentPage());
}
void PageSelector::onCurrentPageChanged(int page)
{
if (m_pageNavigation->pageCount() == 0)
m_pageNumberEdit->setText(QString::number(0));
else
m_pageNumberEdit->setText(QString::number(page + 1));
}
void PageSelector::pageNumberEdited()
{
if (!m_pageNavigation)
return;
const QString text = m_pageNumberEdit->text();
bool ok = false;
const int pageNumber = text.toInt(&ok);
if (!ok)
onCurrentPageChanged(m_pageNavigation->currentPage());
else
m_pageNavigation->setCurrentPage(qBound(0, pageNumber - 1, m_pageNavigation->pageCount() - 1));
}
ZoomSelector.h文件
#ifndef ZOOMSELECTOR_H
#define ZOOMSELECTOR_H
#include <QComboBox>
#include <QPdfView>
class ZoomSelector : public QComboBox
{
Q_OBJECT
public:
explicit ZoomSelector(QWidget *parent = nullptr);
public slots:
void setZoomFactor(qreal zoomFactor);
void reset();
signals:
void zoomModeChanged(QPdfView::ZoomMode zoomMode);
void zoomFactorChanged(qreal zoomFactor);
private slots:
void onCurrentTextChanged(const QString &text);
};
#endif // ZOOMSELECTOR_H
ZoomSelector.cpp文件
#include "zoomselector.h"
#include <QLineEdit>
ZoomSelector::ZoomSelector(QWidget* parent)
: QComboBox(parent)
{
setEditable(true);
addItem(QLatin1String("Fit Width"));
addItem(QLatin1String("Fit Page"));
addItem(QLatin1String("12%"));
addItem(QLatin1String("25%"));
addItem(QLatin1String("33%"));
addItem(QLatin1String("50%"));
addItem(QLatin1String("66%"));
addItem(QLatin1String("75%"));
addItem(QLatin1String("100%"));
addItem(QLatin1String("125%"));
addItem(QLatin1String("150%"));
addItem(QLatin1String("200%"));
addItem(QLatin1String("400%"));
connect(this, static_cast<void(QComboBox::*)(const QString&)>(&QComboBox::currentIndexChanged),
this, &ZoomSelector::onCurrentTextChanged);
connect(lineEdit(), &QLineEdit::editingFinished,
this, [this]() {onCurrentTextChanged(lineEdit()->text()); });
}
void ZoomSelector::setZoomFactor(qreal zoomFactor)
{
setCurrentText(QString::number(qRound(zoomFactor * 100)) + QLatin1String("%"));
}
void ZoomSelector::reset()
{
setCurrentIndex(8); // 100%
}
void ZoomSelector::onCurrentTextChanged(const QString& text)
{
if (text == QLatin1String("Fit Width")) {
emit zoomModeChanged(QPdfView::FitToWidth);
}
else if (text == QLatin1String("Fit Page")) {
emit zoomModeChanged(QPdfView::FitInView);
}
else {
qreal factor = 1.0;
QString withoutPercent(text);
withoutPercent.remove(QLatin1Char('%'));
bool ok = false;
const int zoomLevel = withoutPercent.toInt(&ok);
if (ok)
factor = zoomLevel / 100.0;
emit zoomModeChanged(QPdfView::CustomZoom);
emit zoomFactorChanged(factor);
}
}
小结:
本文主要分享了开发PDF阅读器所依赖的库,以及开发的过程,谢谢各位小伙伴的关注。
感谢各位小伙伴的点赞+关注,小易会继续努力分享,一起进步!
你的赞赏是我的最最最最大的动力(^U^)ノ~YO