I am trying to print a QGLWidget to a printer. When I do print, I only get the background of my QGLWidget with its grid lines. My QGLWidget is a plot with titles, axis labels, and tick marks as well as data and grid lines. You can see when the grid lines print where the data should be because the grid lines are missing in small spots where the data lies on top of the grid lines. mpPrinter is created in my constructor and mpCentralFrame holds a QHBoxLayout which only holds my QGLWidget. Instead of grabbing the window of the frame I have also tried just grabbing the QGLWidget and have received the exact same results. I have set up my print function to look like this:
void myClass::print( )
{
if( mpPrinter == NULL )
{
fprintf( stderr, "No printer available\n" );
}
QPixmap imageToPrint
= QPixmap::grabWindow( mpCentralFrame
->winId
( ) );
QRect rect
= painter.
viewport( );
QSize size
= imageToPrint.
size( );
mpPrinter
->setPrintProgram
( QString( "lp" ) );
mpPrinter
->setColorMode
( QPrinter::Color );
size.scale( rect.size( ), Qt::KeepAspectRatio );
painter.setViewport( rect.x( ), rect.y( ), size.width( ), size.height( ) );
painter.setWindow( imageToPrint.rect( ) );
painter.drawImage( 0, 0, imageToPrint.toImage( ) );
}
void myClass::print( )
{
if( mpPrinter == NULL )
{
fprintf( stderr, "No printer available\n" );
}
QPixmap imageToPrint = QPixmap::grabWindow( mpCentralFrame->winId( ) );
QPainter painter( mpPrinter );
QRect rect = painter.viewport( );
QSize size = imageToPrint.size( );
mpPrinter->setPrintProgram( QString( "lp" ) );
mpPrinter->setColorMode( QPrinter::Color );
size.scale( rect.size( ), Qt::KeepAspectRatio );
painter.setViewport( rect.x( ), rect.y( ), size.width( ), size.height( ) );
painter.setWindow( imageToPrint.rect( ) );
painter.drawImage( 0, 0, imageToPrint.toImage( ) );
}
To copy to clipboard, switch view to plain text mode
Any ideas why my printer only seems to grab the first thing that I draw on the QGLWidget which are the grids. I draw the titles and data after drawing the grid lines by setting up their various viewports on the QGLWidget. Thanks for your help!!
Bookmarks