layout->addWidget(pdfBrowser); 执行这段代码程序就崩溃,,求帮忙
#include "pdfbrowser.h"
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>
#include <QDebug>
PDFBrowser::PDFBrowser(QWidget *parent)
: QWidget{parent}
{
setAttribute(Qt::WA_OpaquePaintEvent);
setAttribute(Qt::WA_StaticContents);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
vScrollBar = new QScrollBar(Qt::Vertical, this);
hScrollBar = new QScrollBar(Qt::Horizontal, this);
vScrollBar->setVisible(false);
hScrollBar->setVisible(false);
m_scrollArea = new QScrollArea(this);
m_scrollArea->setBackgroundRole(QPalette::Base);
m_scrollArea->setAlignment(Qt::AlignCenter);
m_scrollArea->setFrameShape(QFrame::NoFrame);
m_scrollArea->setVerticalScrollBar(vScrollBar);
m_scrollArea->setHorizontalScrollBar(hScrollBar);
m_scrollArea->setWidgetResizable(true);
QVBoxLayout* layout = new QVBoxLayout;
layout->addWidget(m_scrollArea);
setLayout(layout);
connect(vScrollBar, &QScrollBar::valueChanged, this, &PDFBrowser::onScrollBarValueChanged);
connect(hScrollBar, &QScrollBar::valueChanged, this, &PDFBrowser::onScrollBarValueChanged);
document = nullptr;
qDebug()<<"到这里了";
}
PDFBrowser::~PDFBrowser(){
if(m_scrollArea!=nullptr)
delete m_scrollArea;
if(vScrollBar!=nullptr)
delete vScrollBar;
if(hScrollBar!=nullptr)
delete hScrollBar;
if(document!=nullptr)
delete document;
FPDF_DestroyLibrary();
}
void PDFBrowser::resizeEvent(QResizeEvent *event){
//在widget重新变更位置时改变scrollbar
int totalHeight = document->getTotalHeightInPixel();
int maxWidth = document->getMaxWidthInPixel();
vScrollBar->setRange(0, totalHeight - m_scrollArea->height());
hScrollBar->setRange(0, maxWidth - m_scrollArea->width());
redraw();
QWidget::resizeEvent(event);
}
PDFDocument* PDFBrowser::loadPDF(const QString& fileName){
qDebug()<<"hi hi hi555";
FPDF_InitLibrary();
FPDF_DOCUMENT pdocument = FPDF_LoadDocument(fileName.toStdString().c_str(), nullptr);
qDebug()<<"hi hi hi556";
if (pdocument) {
PDFDocument* document = new PDFDocument(pdocument);
qDebug()<<"hi hi hi551";
document->loadAllPage();
this->document = document;
};
redraw();
qDebug()<<"hi hi hi";
return this->document;
}
void PDFBrowser::redraw(){
qDebug()<<"okoi2";
if (document == nullptr) {
return;
}
qDebug()<<"okoi3";
QPixmap result(m_scrollArea->width(), m_scrollArea->height());
QPainter painter(&result);
// 设置背景颜色
painter.setBackground(Qt::darkGray);
painter.setBrush(Qt::darkGray);
// 获取widget大小
int canvasWidth = m_scrollArea->width();
int canvasHeight = m_scrollArea->height();
// 填充矩形区域
painter.drawRect(0, 0, canvasWidth, canvasHeight);
painter.fillRect(0, 0, canvasWidth, canvasHeight, Qt::white);
// Get scrollbar position
int selection = vScrollBar->value();
int hSelection = hScrollBar->value();
// Get canvas size and calculate beginX
QSize canvasSize = m_scrollArea->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
QGraphicsScene scene;
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];
}
painter.setBrush(QColor(Qt::white));
// 绘制矩形并填充背景色
int x = -hSelection + beginX;
int y = 0;
int width = document->getMaxWidthInPixel();
int height = canvasHeight;
painter.drawRect(x, y, width, height);
painter.fillRect(x, y, width, height, painter.brush());
// 当前页应该在这个位置绘制
int maginY = temp - selection;
int paintedCount = 0;
int drawY = maginY;
PDFPage *currentPage = nullptr;
QList<QPixmap> list;
while (paintedCount <=canvasHeight) {
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);
items.append(item);
pageAtY += currentPage->getPageSizeInPixel(this->document->getFPdf_DOCUMENT())[1];
page = document->getPageAtPixel(pageAtY);
// 已经绘制了这么多,离整个clientArea还有一段距离,不够的话接着取下一页
paintedCount += pageHeight + maginY;
// 下一页的坐标(滚动条偏移+当前页大小+偏移),多一个像素去寻找下一页
pageAtY = pageHeight + selection + maginY + 1;
// 下一页画的y坐标
drawY += pageHeight;
}
int px = -hSelection + beginX;
int py = maginY;
// 在绘图设备上绘制所有QPixmap
// foreach (const QPixmap& pixmap, list) {
// painter.drawPixmap(px, py, pixmap);
// int x1 = px;
//int y1 = pixmap.height() + py + 1;
//int x2 = pixmap.width() + px;
//int y2 = y1;
//painter.drawLine(x1, y1, x2, y2);
//py += pixmap.height() + 1; // 两个QPixmap之间加上10px的间距
//}
foreach(QGraphicsPixmapItem* item, items) {
scene.addItem(item);
}
// 在绘图设备上绘制所有QGraphicsPixmapItem
scene.render(&painter);
qDebug()<<"oko5";
QLabel *label = new QLabel();
label->setPixmap(result);
qDebug()<<"oko7";
m_scrollArea->layout()->addWidget(label);
qDebug()<<"oko6";
painter.end();
qDebug()<<"okoi";
}
下面贴上测试类,,
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
QWidget window;
window.setWindowTitle("PDF Browser");
PDFBrowser* pdfBrowser = new PDFBrowser(&window);
pdfBrowser->loadPDF("D:\\pdfD\\040300122068.pdf");
QVBoxLayout* layout = new QVBoxLayout;
layout->addWidget(pdfBrowser);
window.setLayout(layout);
window.show();;
return a.exec();
}