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. Trying to render from a QGraphicsScene to a printer...
Forum Updated to NodeBB v4.3 + New Features

Trying to render from a QGraphicsScene to a printer...

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 2.5k 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.
  • P Offline
    P Offline
    PSI-lbc
    wrote on last edited by
    #1

    Trying to render a scene to a printer via a painter...

    The scene gets populated and displays at the .show. in the QGraphicsView widget on the ui form.

    The print dialog opens, allowing a printer to be selected.

    A blank sheet is all that is output.

    Not sure what I'm doing wrong. Tried googling and forum searching for examples on how to do this. The docs have "examples" but are pretty simplistic.

    Any help would be appreciated.

    @
    void frmSightTape::on_pbTest_clicked()
    {

    QString filePath = QApplication::applicationDirPath() + "/Graphics/";

    QFont sceneFont("Arial", 10, QFont::Bold);
    qreal pdpi = 600;
    QGraphicsScene scene = new QGraphicsScene(0,0,pdpi8.5,pdpi*11.0);

    QGraphicsPixmapItem *tape = new QGraphicsPixmapItem(0,0);
    tape->setPixmap( QPixmap(filePath + "tmpTapeScaled.png",0,Qt::AutoColor ));

    tape->setPos(100,100);
    tape->setScale( 1 );
    scene->addItem( tape );

    QGraphicsTextItem *bla = new QGraphicsTextItem( "bla bla bla",0,0);
    bla->setPos( 20,20 );
    scene->addItem( bla );

    // gvTapeSheet is a QGraphicsView on a ui form

    ui->gvTapeSheet->setScene( scene );
    ui->gvTapeSheet->show();

    // the tape sheet changes to reflect the items added

    QPrinter printer(QPrinter::HighResolution);
    //printer.setPaperSize(QPrinter::Letter);
    printer.setPageSize(QPrinter::Letter);

    QPrintDialog *printDialog = new QPrintDialog(&printer, this);
    if (printDialog->exec() == QDialog::Accepted)
    {

    QPainter painter(&printer);
    painter.setRenderHint(QPainter::Antialiasing);
    
    #if DEBUG_PRINT
      qDebug() << "-on_pbTest: scene  w=" &lt;&lt; scene-&gt;sceneRect().width() &lt;&lt; " h=" &lt;&lt; scene-&gt;sceneRect().height();
      qDebug() &lt;&lt; "-on_pbTest: printer " << printer.paperRect();
      qDebug() << "-on_pbTest: printer " << printer.resolution();
    #endif
    
    scene->render(&painter,scene->sceneRect(),printer.paperRect(),Qt::KeepAspectRatio);
    //scene->render(&painter);
    //ui->gvLabelSheet->scene()->render(&painter,ui->gvLabelSheet->scene()->sceneRect(),printer.paperRect(),Qt::KeepAspectRatio);
    //ui->gvLabelSheet->scene()->render(&painter);
    
    painter.end();
    

    }

    }

    @

    debug output is..

    -on_pbTest: scene w= 5100 h= 6600
    -on_pbTest: printer QRect(0,0 5100x6600)
    -on_pbTest: printer 600

    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