Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Appending multiple images with QPainter and poppler results in black pages
Forum Updated to NodeBB v4.3 + New Features

Appending multiple images with QPainter and poppler results in black pages

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 2.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Threadcore
    wrote on last edited by
    #1

    Hello there

    I wrote a very basic PDF reader, based on poppler. The idea is to read every page, convert it to an QImage and in the end append it. The problem is, that afer page 24 I just see black pages. Is this a limitation in QPainter/QImage or did I make a mistake ? Is there a better way to append images or can anybody recommend a good Qt PDF library (GPL3) ?

    @#include "FastLatexPDFWidget.h"

    FastLatexPDFWidget::FastLatexPDFWidget(const QString &filepath, QWidget *parent) : QLabel(parent)
    {
    Poppler::Document *document = Poppler::Document::load(filepath);
    if(document && !document->isLocked())
    {
    // Document attributes
    int maxwidth = 0;
    int maxheight = 0;

    // Contains each page as QImage (1 QImage = 1 Page)
    QList<QImage> listImages;

    // Iterate over the document
    for(int i = 0; i < document->numPages(); i++)
    {
    // Load the page as QImage and append it to the list
    Poppler::Page *page = document->page(i);
    QImage image = page->renderToImage(physicalDpiX(), physicalDpiY());
    listImages.append(image);

    // Release the page
    delete page;

    int width = image.width();
    int height = image.height();

    // Vertical or horizontal orientation - take care of that and increase the width
    if(width > maxwidth)
    {
    maxwidth = width;
    }

    // Add height
    maxheight += height;
    }

    // Now create the output image
    QImage output(maxwidth, maxheight, QImage::Format_RGB32);

    // Create a painter object and set it to the output image
    QPainter painter;
    painter.begin(&output);

    // Attribute for the passed height during drawing
    int passedheight = 0;

    // Now draw the images from the list
    for(int i = 0; i < listImages.count(); i++)
    {
    QImage image = listImages.at(i);
    painter.drawImage(0, passedheight, image);
    passedheight += image.height();
    }

    // Now stop the painter
    painter.end();

    // Add the image to the label
    setPixmap(QPixmap::fromImage(output));

    // Release the document
    delete document;

    // @TESTING
    Q_ASSERT(maxheight == passedheight);
    }
    }
    @

    Thanks in advice

    M 1 Reply Last reply
    0
    • T Threadcore

      Hello there

      I wrote a very basic PDF reader, based on poppler. The idea is to read every page, convert it to an QImage and in the end append it. The problem is, that afer page 24 I just see black pages. Is this a limitation in QPainter/QImage or did I make a mistake ? Is there a better way to append images or can anybody recommend a good Qt PDF library (GPL3) ?

      @#include "FastLatexPDFWidget.h"

      FastLatexPDFWidget::FastLatexPDFWidget(const QString &filepath, QWidget *parent) : QLabel(parent)
      {
      Poppler::Document *document = Poppler::Document::load(filepath);
      if(document && !document->isLocked())
      {
      // Document attributes
      int maxwidth = 0;
      int maxheight = 0;

      // Contains each page as QImage (1 QImage = 1 Page)
      QList<QImage> listImages;

      // Iterate over the document
      for(int i = 0; i < document->numPages(); i++)
      {
      // Load the page as QImage and append it to the list
      Poppler::Page *page = document->page(i);
      QImage image = page->renderToImage(physicalDpiX(), physicalDpiY());
      listImages.append(image);

      // Release the page
      delete page;

      int width = image.width();
      int height = image.height();

      // Vertical or horizontal orientation - take care of that and increase the width
      if(width > maxwidth)
      {
      maxwidth = width;
      }

      // Add height
      maxheight += height;
      }

      // Now create the output image
      QImage output(maxwidth, maxheight, QImage::Format_RGB32);

      // Create a painter object and set it to the output image
      QPainter painter;
      painter.begin(&output);

      // Attribute for the passed height during drawing
      int passedheight = 0;

      // Now draw the images from the list
      for(int i = 0; i < listImages.count(); i++)
      {
      QImage image = listImages.at(i);
      painter.drawImage(0, passedheight, image);
      passedheight += image.height();
      }

      // Now stop the painter
      painter.end();

      // Add the image to the label
      setPixmap(QPixmap::fromImage(output));

      // Release the document
      delete document;

      // @TESTING
      Q_ASSERT(maxheight == passedheight);
      }
      }
      @

      Thanks in advice

      M Offline
      M Offline
      moozoom
      wrote on last edited by
      #2

      @Threadcore

      I think there is no limitation.

      Add to your image output a background color, like this:

      // Now create the output image
      QImage output(maxWidth, maxHeight, QImage::Format_RGB32);
      output.fill(Qt::white);

      In my case, this is a good solution.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved