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. Print Widget to PDF with the minimal content

Print Widget to PDF with the minimal content

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 963 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.
  • A Offline
    A Offline
    AmirHammoutene
    wrote on last edited by
    #1

    Hello,

    I print a widget (a tab from a QTabWidget) with its content with this code :

    void MainWindow::print()
    {
        quint8 tabIndex = quint8(ui->tabWidget->currentIndex());
        CharacterSheetWidget* widget = dynamic_cast< CharacterSheetWidget* >( ui->tabWidget->widget(tabIndex) );
        QString tabName = ui->tabWidget->tabText(tabIndex);
        QString filePath = QFileDialog::getSaveFileName(this, QString(tr("Print as PDF...")), QString(), "PDF file (*.pdf)");
    
        if(filePath.isEmpty())
            return;
    
        QPrinter printer;
        printer.setOutputFormat(QPrinter::PdfFormat);
        printer.setOutputFileName(filePath);
        printer.setPaperSize(QPrinter::A4);
        printer.setResolution(300);
        printer.setFullPage(false);
        printer.setPageMargins(5, 5, 5, 5, QPrinter::Millimeter);
    
        QString originalStyle = widget->styleSheet();
        widget->setStyleSheet("background-color:white;");
    
        QPainter painter;
        painter.begin(&printer);
        double xscale = printer.pageRect().width() / double(widget->width());
        double yscale = printer.pageRect().height() / double(widget->height());
        double scale = qMin(xscale, yscale);
        painter.translate(printer.paperRect().center());
        painter.scale(scale, scale);
        painter.translate(- widget->width()/ 2, - widget->height()/ 2);
        painter.setRenderHint(QPainter::Antialiasing, true);
        painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
        widget->render(&painter);
        painter.end();
        widget->setStyleSheet(originalStyle);
    }
    

    but it printed it with a lot of space on the right and on bottom.
    I would like to print the minimal content.
    For exemple on this image, you see that it prints the red area in the pdf, I would like it prints the blue area.
    How should I do (if you need more information, please tell me)

    alt text

    1 Reply Last reply
    0
    • A Offline
      A Offline
      AmirHammoutene
      wrote on last edited by
      #2

      By the way, I founded a solution : Before "QPainter painter;", I added this code :

      QSize minSize = widget->minimumSizeHint();
      int minWidth = minSize.width();
      int minHeight = minSize.height();
      

      And replaced all widget->width() and widget->height by minWidth and minHeight.

      solved

      1 Reply Last reply
      2

      • Login

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