QGraphicsPixmapItem 不显示图像
可以帮忙吗?我只是得到一个带有垂直滚动条的空白视图。可以查一下代码吗?我认为问题始于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
乍一看我看不出这有什么问题。
然而,冒着陈述显而易见的风险,您确定 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?