#include "PDFBrow.h"
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>
#include <QGraphicsView>
#include <QLineF>
#include <QDebug>
PDFBrow::PDFBrow(QWidget *parent)
: QWidget{parent}
{ //关闭在析构函数里呢
FPDF_InitLibrary();
setAttribute(Qt::WA_OpaquePaintEvent);
setAttribute(Qt::WA_StaticContents);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
m_scrollArea = new QScrollArea(this);
m_scrollArea->setBackgroundRole(QPalette::Base);
m_scrollArea->setAlignment(Qt::AlignCenter);
m_scrollArea->setWidgetResizable(false);
m_scrollArea->setFrameShape(QFrame::NoFrame);
view = new QGraphicsView();
view->setVerticalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOn);
view->setFrameShape(QFrame::NoFrame);
view->setVerticalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff);
view->setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff);
view->setSceneRect(0, 0, view->viewport()->width(), view->viewport()->height());
view->setAlignment(Qt::AlignLeft | Qt::AlignTop);
view->setFixedSize(this->parentWidget()->size());
vScrollBar = m_scrollArea->verticalScrollBar();
hScrollBar = m_scrollArea->horizontalScrollBar();
scene = new QGraphicsScene() ;
m_scrollAreaContentLayout = new QVBoxLayout;
m_scrollAreaContentLayout->addWidget(view);
m_scrollAreaContentLayout->addStretch();
layout_di = new QVBoxLayout;
m_scrollArea->viewport()->setLayout(m_scrollAreaContentLayout);
layout_di->addWidget(m_scrollArea);
this->setLayout(layout_di);
connect(vScrollBar, &QScrollBar::valueChanged, this, &PDFBrow::onScrollBarValueChanged);
connect(hScrollBar, &QScrollBar::valueChanged, this, &PDFBrow::onScrollBarValueChanged);
document = nullptr;
}
void PDFBrow::onScrollBarValueChanged(){
redraw();
}
PDFBrow::~PDFBrow(){
if(m_scrollArea)
delete m_scrollArea;
if(vScrollBar)
delete vScrollBar;
if(hScrollBar)
delete hScrollBar;
if(document)
delete document;
if(scene)
delete scene;
FPDF_DestroyLibrary();
}
void PDFBrow::resizeEvent(QResizeEvent *event){
//在widget重新变更位置时改变scrollba
QWidget::resizeEvent(event);
// this->view ->resize(this->size());
}
PDFDocument* PDFBrow::loadPDF(const QString& fileName){
FPDF_DOCUMENT pdocument = FPDF_LoadDocument(fileName.toStdString().c_str(), nullptr);
if (pdocument) {
PDFDocument* document = new PDFDocument(pdocument);
document->loadAllPage();
this->document = document;
int totalHeight = document->getTotalHeightInPixel();
int maxWidth = document->getMaxWidthInPixel();
vScrollBar->setSingleStep(20);
vScrollBar->setPageStep(20);
hScrollBar->setSingleStep(20);
hScrollBar->setPageStep(20);
vScrollBar->setVisible(true);
hScrollBar->setVisible(true);
vScrollBar->setRange(0, totalHeight - view->height());
hScrollBar->setRange(0, maxWidth - view->width());
};
redraw();
return this->document;
}
void PDFBrow::redraw(){
if (document == nullptr) {
return;
}
// 获取widget大小
int canvasWidth = view->width();
int canvasHeight = this->view->viewport()->height();
qDebug()<<"canvasHeight:"+QString::number(canvasHeight);
int selection = vScrollBar->value();
int hSelection = hScrollBar->value();
QSize canvasSize = view->viewport()->size();
//居中
int beginX = 0;
if (canvasSize.width() > document->getMaxWidthInPixel()) {
beginX = (canvasSize.width() - document->getMaxWidthInPixel()) / 2;
}
// Draw PDF pages to QPixmap and show it on canvas
QList<QGraphicsPixmapItem*> items;
int pageAtY = selection;
PDFPage *page = document->getPageAtPixel(pageAtY);
//获取当前页是第几页
int index = page->getIndex();
//前几页的总长度
int temp = 0;
for(int i=0;i<index;i++){
temp+=document->getAllPage()[i].getPageSizeInPixel(document->getFPdf_DOCUMENT())[1];
}
// 当前页应该在这个位置绘制
int maginY = temp - selection;
int paintedCount = 0;
int drawY = maginY;
PDFPage *currentPage = nullptr;
while (paintedCount <=canvasHeight) {
page = document->getPageAtPixel(pageAtY);
if(page==nullptr)
break;
if (currentPage == page) {
break;
}
currentPage = page;
int pageWidth = currentPage->getPageSizeInPixel(this->document->getFPdf_DOCUMENT())[0];
int pageHeight = currentPage->getPageSizeInPixel(this->document->getFPdf_DOCUMENT())[1];
QGraphicsPixmapItem* item = new QGraphicsPixmapItem(currentPage->render(-hSelection + beginX, drawY, pageWidth, pageHeight, 0, 0));
item->setPos(-hSelection + beginX, drawY+1);
items.append(item);
drawY += pageHeight;
// 已经绘制了这么多,离整个clientArea还有一段距离,不够的话接着取下一页
qDebug()<<"pageWidth:"+QString::number(pageWidth)+"pageInDex:"+QString::number(page->getIndex())+"本页pageAtY:"+QString::number(pageAtY)+"pageHeight:"+QString::number(pageHeight)+"maginY:"+QString::number(maginY)+" selection:"+QString::number(selection);
// 下一页的坐标(滚动条偏移+当前页大小+偏移),多一个像素去寻找下一页
pageAtY += pageHeight + 1;
paintedCount = drawY ;
// 下一页画的y坐标
}
scene->clear();
foreach(QGraphicsPixmapItem* item, items) {
scene->addItem(item);
}
view->setScene(scene);
}
边框中的黑边如何去掉,qt
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- CSDN专家-sinJack 2023-03-20 20:29关注
在PDFBrow类的构造函数中添加以下代码试试
QPalette p = palette(); p.setColor(QPalette::Window, Qt::white); setPalette(p); setStyleSheet("PDFBrow { border-style: none; }");
将设置窗口背景为白色,并将边框样式设置为无。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报 编辑记录
悬赏问题
- ¥15 vue请求不到数据,返回状态200,数据为html
- ¥15 访问url时不会自动调用其 Servlet的doGet()
- ¥15 用白鹭引擎开发棋牌游戏的前端为什么这么难找
- ¥15 MATLAB解决问题
- ¥35 哪位专业人士知道这是什么原件吗?哪里可以买到?
- ¥15 关于#c##的问题:treenode反序列化后获取不到上一节点和下一节点,Fullpath和Handle报错
- ¥15 一部手机能否同时用不同的app进入不同的直播间?
- ¥15 没输出运行不了什么问题
- ¥20 输入import torch显示Intel MKL FATAL ERROR,系统驱动1%,: Cannot load mkl_intel_thread.dll.
- ¥15 点云密度大则包围盒小