Hi,

Can someone please verify this? Click the 3rd button on the viewer, and print to a file, it appears to be a bug.

Qt Code:
  1. #include <QtGui>
  2.  
  3. class MyGraphicsView : public QGraphicsView {
  4. public:
  5. MyGraphicsView( QGraphicsScene * scene ) :
  6. QGraphicsView( scene ) {
  7. }
  8.  
  9. protected:
  10.  
  11. virtual void contextMenuEvent ( QContextMenuEvent * event ) {
  12. al << "Print..." << "Save as an image file...";
  13.  
  14. QMenu menu;
  15. for ( QStringList::const_iterator it=al.begin(); it!=al.end(); it++ ) {
  16. menu.addAction( (*it) );
  17. }
  18.  
  19. QAction* action = menu.exec( event->globalPos() );
  20. if ( action ) {
  21. takeAction( al.indexOf( action->text() ) );
  22. }
  23.  
  24. }
  25.  
  26. private:
  27.  
  28. void takeAction( int idx ) {
  29. switch ( idx ) {
  30. case 0: {
  31. QPrinter printer( QPrinter::HighResolution );
  32. printer.setDocName( "test" );
  33. printer.setCreator( "test example" );
  34. printer.setOrientation( QPrinter::Landscape );
  35. QPrintDialog dialog( &printer );
  36. if ( dialog.exec() ) {
  37. QPainter painter( &printer );
  38. scene()->render( &painter );
  39. }
  40. } break;
  41. case 1: {
  42. QString filter( "*.bmp *.jpg *.jpeg *.png *.ppm *.tiff *.xbm *.xpm" );
  43. QString filename = QFileDialog::getSaveFileName( this, "Save as image",
  44. "", filter );
  45. if ( !filename.isEmpty() ) {
  46. QRectF frame( scene()->sceneRect() );
  47. frame.moveTo( 0., 0. );
  48. QImage image( frame.size().toSize(), QImage::Format_RGB32 );
  49. QPainter painter( &image );
  50. painter.fillRect( frame, QBrush( Qt::white ) );
  51. scene()->render( &painter );
  52. if ( !image.save( filename ) ) {
  53. qDebug() << "Fail to save file";
  54. }
  55. }
  56. } break;
  57. }
  58.  
  59. }
  60.  
  61. };
  62.  
  63. class MyImageItem : public QGraphicsItem {
  64. public:
  65.  
  66. MyImageItem( QGraphicsItem* parent=0 ) :
  67. QGraphicsItem( parent ) {
  68. }
  69.  
  70. QRectF boundingRect() const {
  71. return QRectF( 0, 0, 400, 400 );
  72. }
  73.  
  74. void paint( QPainter *painter,
  75. const QStyleOptionGraphicsItem *option,
  76. QWidget* widget ) {
  77.  
  78. QPen pen( Qt::red );
  79. painter->setPen( pen );
  80.  
  81. // create a mono image
  82. QImage image( boundingRect().size().toSize(), QImage::Format_Mono );
  83.  
  84. // set color1 to be transparent and fill the image
  85. image.setColor( Qt::color1, Qt::transparent );
  86. image.fill( Qt::color1 );
  87.  
  88. // set foreground color
  89. image.setColor( Qt::color0, pen.color().rgb() );
  90.  
  91. for ( int ix = 0; ix < image.width(); ix++ ) {
  92. image.setPixel( QPoint( ix, 1 ), Qt::color0 );
  93. image.setPixel( QPoint( ix, image.height()/2 ), Qt::color0 );
  94. image.setPixel( QPoint( ix, image.height() - 1 ), Qt::color0 );
  95. }
  96.  
  97. for ( int iy = 0; iy < image.height(); iy++ ) {
  98. image.setPixel( QPoint( 1, iy ), Qt::color0 );
  99. image.setPixel( QPoint( image.width()/2, iy ), Qt::color0 );
  100. image.setPixel( QPoint( image.width()-1, iy ), Qt::color0 );
  101. }
  102.  
  103. painter->drawImage( image.rect(), image );
  104. //painter->drawPixmap( QPoint( 0, 0 ), QBitmap::fromImage( image ) );
  105.  
  106. }
  107.  
  108. };
  109.  
  110. int main( int argc, char** argv )
  111. {
  112. QApplication app( argc, argv );
  113. scene->setSceneRect( QRectF( 0, 0, 450, 450 ) );
  114.  
  115. MyImageItem imgItem;
  116. imgItem.setPos( 50, 50 );
  117.  
  118. scene->addItem( &imgItem );
  119.  
  120. // view
  121. MyGraphicsView view( scene );
  122. view.resize( 500, 500 );
  123. view.show();
  124.  
  125. return app.exec();
  126.  
  127. }
To copy to clipboard, switch view to plain text mode