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. A few questions about pdf file generation using render & QPdfWriter & QCustomplot
Forum Updated to NodeBB v4.3 + New Features

A few questions about pdf file generation using render & QPdfWriter & QCustomplot

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 243 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.
  • J Offline
    J Offline
    jcqtter
    wrote on 28 Nov 2023, 08:13 last edited by
    #1

    Qt Version :5.8
    OS : Windows

    My Question:

    1、
    I want to export the entire widget to pdf,I find the render method,So here's the code

    QFile file("D:/test.pdf");
    file.open(QIODevice::Append);
    QPdfWriter* pdf= new QPdfWriter(&file);
    pdf->setPageSize(QPagedPaintDevice::A4);
    pdf->setResolution(QPrinter::ScreenResolution);
    QPainter printpainter;
    printpainter.begin(pdf);
    double scale = (double)printer->width() / m_wgtRect.width();
    printpainter.scale(scale, scale);
    m_wgtRect->render(&printpainter);
    printpainter.end()
    

    But there are images and qcustomplot in the widget,After they go through render, they will not be clear when zoomed in,So how do I handle images and qcustomplot in render?

    2、
    Notice the savePdf method in qcustomplot,Look at the source code and found,What he was using was QPrinter & QPainter,I replaced the QPrinter with a QPdfWriter,The text ratio in the generated pdf file is wrong and becomes very large, especially axis labelText,Further trace code I find the difference:

    QPrinter* printer = new QPrinter(QPrinter::ScreenResolution);
    printer->setOutputFormat(QPrinter::PdfFormat);
    printer->setPageSize(QPagedPaintDevice::A4);
    printer->setOutputFileName("D://test2.pdf");
    
    QPainter painter2(printer);
    auto rt2 = painter2.fontMetrics().boundingRect(0, 0, 0, 0, Qt::TextDontClip, "x");
    
    painter2.end();
    
    

    and

    QPdfWriter* pdf = new QPdfWriter("D://test.pdf");
    pdf->setResolution(QPrinter::ScreenResolution);
    pdf->setPageSize(QPagedPaintDevice::A4);
    	
    QPainter painter1(pdf);
    auto rt1 = painter1.fontMetrics().boundingRect(0, 0, 0, 0, Qt::TextDontClip, "x");
    
    painter1.end();
    
    

    Just use a different device to initialize QPainter,The rect of the resulting font is different:
    Use QPrinter : (0,0,6,12)
    Use QPdfWriter: (0,0,75,150)
    Even if the setFont method is used to modify the font of QPdfWriter, it will not work,So, how can QPdfWriter get the same font rect as QPrinter?

    Best wishes

    1 Reply Last reply
    0

    1/1

    28 Nov 2023, 08:13

    • Login

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