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 : Error with Icon
QtWS25 Last Chance

Print Widget to PDF : Error with Icon

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 490 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
    AmirHammoutene
    wrote on last edited by
    #1

    Hello,

    I try to print to PDF an entire widget that can contain multiple sort of widgets.
    The widget to be printed is a tab contained in a QTabWidget.

    Here is the code that tries to print :

    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...")), tabName, "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);
    }
    

    As I said, the user can add multiple type of widgets.
    When the entire widget contains QLabel, QLineEdit, QSpinBox, and even QLabel with a QPixmap, the PDF is generated with no problem.
    Here is how I generate the QLabel with QPixmap (the print works)

            QPixmap pix;
            QByteArray bytes = QByteArray::fromBase64(info.imageFileData.toLatin1());
            pix.loadFromData(bytes, "PNG");
            if(!pix.isNull())
            {
                pix = pix.scaled(300, 100, Qt::KeepAspectRatio);
                displayWidget->ui->icon->setPixmap(QPixmap(pix));
            }
    

    info.imageFileData being a QString.

    So for the moment everything works.

    But, when the user add a QPushButton which contains an icon (icon file that is attributed to the QPushButton via Qt Designer : I added a ressource file, added the .png image, and browsed from Qt Designer to that png file under the property "icon" )
    ... the print crashes the program, I have a 0 ko pdf and this message :

    QPainter::begin: Paint device returned engine == 0, type: 2
    QPainter::setRenderHint: Painter must be active to set rendering hints
    QPainter::setWorldTransform: Painter not active
    QWidget::render: Cannot render with an inactive painter
    QPainter::end: Painter not active, aborted
    terminate called after throwing an instance of 'std::bad_alloc'
      what():  std::bad_alloc
    

    by debugging, I see that it crash at "painter.end();" line

    what's wrong ?
    (if you need more information, please tell me)

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Please provide a minimal testcase so we can reproduce a crash. From a first pov it should not crash. Also which OS and Qt version do you use?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

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

        I just wrote a minimal testcase, with a lot of the same conditions from my code, but the print is doin' good.
        I don't know what to do ... ?

        Windows 10,Qt 5.13.2

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

          Ok, I clean the project, run qmake, recompile and it works...

          solved

          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