开发者

QGraphicsPixmapItem not displaying image

开发者 https://www.devze.com 2023-03-23 21:45 出处:网络
Can help? I just get a blank view with vertical scrollbar. Can check the code? I think the trouble starts at item.setPixmap(pixmap); because the pixmap is the same size of image that is the image is l

Can help? I just get a blank view with vertical scrollbar. Can check the code? I think the trouble starts at item.setPixmap(pixmap); because the pixmap is the same size of image that is the image is loaded into it. thanks

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <poppler-qt4.h>
#include <QGraphicsPixmapItem>
#include <QGraphicsRotation>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
ui->setupUi(this);

QString filename;

Poppler::Document* document = Poppler::Document::load("/home/pc/Documenti/ICS-TR-06-16.pdf");
if (!document || document->isLocked())
{

  // ... error message ....

  delete document;
  return;
}

qDebug("doc %i \n",document->numPages()); //gives 11

// Access page of the PDF file
Poppler::Page* pdfPage = document->page(1);  // Document starts at page 0
if (pdfPage == 0) {
  // ... error message ...
  return;
}

// Generate a QImage of the rendered page
QImage image = pdfPage->renderToImage(72.0,72.0,0,0,pdfPage->pageSize().width(),pdfPage->pageSize().height());
if (image.isNull()) {
  // ... error message ...
  return;
}

qDebug("img %i %i \n",image.width(),image.height()); //gives 612 792
QGraphicsScene* scene=new QGraphicsScene();


QPixmap pixmap;
pixmap=QPixmap::fromImage(image);
qDebug("pix %i %i \n",pixmap.width(),pixmap.height()); //gives 612 792



QGraphicsPixmapItem item;
item.setPixmap(pixmap);
qDebug("itpix %i %i \n",item.pixmap().width(),item.pixmap().height()); //gives 612 792
//qDebug("ite %i %i \n",);

    scene->addItem(&item);
    this->ui->graphicsView->setScene(sc开发者_StackOverflow社区ene);

 this->ui->graphicsView->show();
// ... use image ...

// after the usage, the page must be deleted
delete pdfPage;
}

MainWindow::~MainWindow()
{
if (document!=0) delete document;
delete ui;
}    


I can't see anything wrong with this at first glance.

However, at the risk of stating the obvious, are you sure the first page of your PDF isn't blank?

0

精彩评论

暂无评论...
验证码 换一张
取 消