Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-54196

Rendering QWidget to QPainter does not always work

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Incomplete
    • Icon: P2: Important P2: Important
    • None
    • 5.7.0
    • GUI: Printing
    • None
    • Windows 10 x64

      Hello,
      I am trying to print a QWidget, exactly: a QTableView:

        QPrinter printer;
      
        QPrintDialog dialog(&printer, this);
        dialog.setWindowTitle(tr("Print Document"));
        if (dialog.exec() != QDialog::Accepted) {
          return;
        }
      
      
        QPainter painter;
      
        bool success = painter.begin(&printer);
        Q_ASSERT(success);
      
        double xscale = printer.pageRect().width()/double(ui->tableViewAnleitung->width());
        double yscale = printer.pageRect().height()/double(ui->tableViewAnleitung->height());
        double scale = qMin(xscale, yscale);
        painter.translate(printer.paperRect().x() + printer.pageRect().width()/2,
                          printer.paperRect().y() + printer.pageRect().height()/2);
        painter.scale(scale, scale);
        painter.translate(-width()/2, -height()/2);
      
        ui->tableViewAnleitung->render(&painter);
        painter.end()
      

      This gives the following error messages:

      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
      

      They are released in qwidget.cpp, lines 5468-5471:

              // Create a pixmap device coordinate painter.
              QPainter pixmapPainter(&pixmap);
              pixmapPainter.setRenderHints(painter->renderHints());
              transform *= QTransform::fromTranslate(-deviceRect.x(), -deviceRect.y());
              pixmapPainter.setTransform(transform);
      

      When I use a QPainter that is not using a printer but a pixmap to paint the widget, then it works. I can then print the pixmap by the given example above.

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            johnlayt John Layt
            topse Tobias Steinmann
            Votes:
            2 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes