Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QPrinter with QPainter, Painting an image from qml to pdf
Qt 6.12 LTS is out! See what's new in the release blog

QPrinter with QPainter, Painting an image from qml to pdf

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 1.7k Views
  • 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.
  • A Offline
    A Offline
    arozon
    wrote on last edited by
    #1

    Hi i am having some difficulty painting an image with QPainter, I am calling my function from qml, my image size is A4 regular format which is an image generated from a grabToImage.

    I am passing the arguments filename, and imagesourceurl (The grabtoImage result url..)

    I am working in qml and i have a custom plugin that handles all my FileIO, databases, however for some reason i cant get this to work... The image size is the proper pixel size of a full A4 sheet so i am expecting the image to simply cover the area and keep things simple.

    Component size is 2550x - 3300y pixels

    bool FileIO::printToPDF(const QString& imgfilename, const QString& filename) {
        QImage img(imgfilename, Q_NULLPTR);
        QPrinter printer(QPrinter::HighResolution);
        if (filename.isEmpty()) return false;
        printer.setPaperSize(QPrinter::A4);
        printer.setFullPage(true);
        printer.setOutputFileName(filename + ".pdf");
        printer.setOutputFormat(QPrinter::PdfFormat);
        QPainter paint(&printer);
        paint.drawImage(0,0,img);
        paint.end();
        return true;
    }
    

    I am getting a pdf document created in the correct working directory form my FileIO plugin however the document is blank... What am I missing. I cant seem to find a lot of examples on using QPrinter and QPainter

    jsulmJ 1 Reply Last reply
    0
    • A arozon

      Hi i am having some difficulty painting an image with QPainter, I am calling my function from qml, my image size is A4 regular format which is an image generated from a grabToImage.

      I am passing the arguments filename, and imagesourceurl (The grabtoImage result url..)

      I am working in qml and i have a custom plugin that handles all my FileIO, databases, however for some reason i cant get this to work... The image size is the proper pixel size of a full A4 sheet so i am expecting the image to simply cover the area and keep things simple.

      Component size is 2550x - 3300y pixels

      bool FileIO::printToPDF(const QString& imgfilename, const QString& filename) {
          QImage img(imgfilename, Q_NULLPTR);
          QPrinter printer(QPrinter::HighResolution);
          if (filename.isEmpty()) return false;
          printer.setPaperSize(QPrinter::A4);
          printer.setFullPage(true);
          printer.setOutputFileName(filename + ".pdf");
          printer.setOutputFormat(QPrinter::PdfFormat);
          QPainter paint(&printer);
          paint.drawImage(0,0,img);
          paint.end();
          return true;
      }
      

      I am getting a pdf document created in the correct working directory form my FileIO plugin however the document is blank... What am I missing. I cant seem to find a lot of examples on using QPrinter and QPainter

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @arozon You should check whether img is a valid picture with http://doc.qt.io/qt-5/qimage.html#isNull

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1

      • Login

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