QGraphicsPixmapItem 不显示图像

发布于 2024-11-27 10:15:58 字数 1788 浏览 0 评论 0原文

可以帮忙吗?我只是得到一个带有垂直滚动条的空白视图。可以查一下代码吗?我认为问题始于 item.setPixmap(pixmap);因为像素图与图像加载到其中的图像大小相同。谢谢

#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(scene);

 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;
}    

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(scene);

 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;
}    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慕巷 2024-12-04 10:15:58

乍一看我看不出这有什么问题。

然而,冒着陈述显而易见的风险,您确定 PDF 的第一页不是空白的吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文