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

Bug when printing mono image to pdf with transparent background

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: P2: Important P2: Important
    • 4.6.0
    • 4.5.0
    • GUI: Printing
    • None
    • 0a90113262eab4e05bf73c0b69b1f5633a10e768

      I create a mono image with two colors: foreground color (Qt::color0) is
      red, and background color (Qt::color1) is transparent.

      I can save it to an image file without problem. However, when I print it
      to a PDF file, the transparent background becomes all black.

      This seems to be similar to the issue faced in task #242917

      Example:

      #include <QtGui>

      class MyGraphicsView : public QGraphicsView {
      public:
      MyGraphicsView( QGraphicsScene * scene ) :
      QGraphicsView( scene ) {
      }

      protected:

      virtual void contextMenuEvent ( QContextMenuEvent * event ) {
      QStringList al;
      al << "Print..." << "Save as an image file...";

      QMenu menu;
      for ( QStringList::const_iterator it=al.begin(); it!=al.end(); it++ )

      { menu.addAction( (*it) ); }

      QAction* action = menu.exec( event->globalPos() );
      if ( action )

      { takeAction( al.indexOf( action->text() ) ); }

      }

      private:

      void takeAction( int idx ) {
      switch ( idx ) {
      case 0: {
      QPrinter printer( QPrinter::HighResolution );
      printer.setDocName( "test" );
      printer.setCreator( "test example" );
      printer.setOrientation( QPrinter::Landscape );
      QPrintDialog dialog( &printer );
      if ( dialog.exec() )

      { QPainter painter( &printer ); scene()->render( &painter ); }

      } break;
      case 1: {
      QString filter( "*.bmp *.jpg *.jpeg *.png *.ppm *.tiff *.xbm *.xpm" );
      QString filename = QFileDialog::getSaveFileName( this, "Save as image",
      "", filter );
      if ( !filename.isEmpty() ) {
      QRectF frame( scene()->sceneRect() );
      frame.moveTo( 0., 0. );
      QImage image( frame.size().toSize(), QImage::Format_RGB32 );
      QPainter painter( &image );
      painter.fillRect( frame, QBrush( Qt::white ) );
      scene()->render( &painter );
      if ( !image.save( filename ) )

      { qDebug() << "Fail to save file"; }

      }
      } break;
      }

      }

      };

      class MyImageItem : public QGraphicsItem {
      public:

      MyImageItem( QGraphicsItem* parent=0 ) :
      QGraphicsItem( parent ) {
      }

      QRectF boundingRect() const

      { return QRectF( 0, 0, 400, 400 ); }

      void paint( QPainter *painter,
      const QStyleOptionGraphicsItem *option,
      QWidget* widget ) {

      QPen pen( Qt::red );
      painter->setPen( pen );

      QFont font = painter->font();
      font.setPixelSize( 16 );

      painter->setFont( font );
      painter->drawText( 30, 30, "Text behind a transparent image" );

      // create a mono image
      QImage image( boundingRect().size().toSize(), QImage::Format_Mono );

      // set color1 to be transparent and fill the image
      image.setColor( Qt::color1, Qt::transparent );
      image.fill( Qt::color1 );

      // set foreground color
      image.setColor( Qt::color0, pen.color().rgb() );

      for ( int ix = 0; ix < image.width(); ix++ )

      { image.setPixel( QPoint( ix, 1 ), Qt::color0 ); image.setPixel( QPoint( ix, image.height()/2 ), Qt::color0 ); image.setPixel( QPoint( ix, image.height() - 1 ), Qt::color0 ); }

      for ( int iy = 0; iy < image.height(); iy++ )

      { image.setPixel( QPoint( 1, iy ), Qt::color0 ); image.setPixel( QPoint( image.width()/2, iy ), Qt::color0 ); image.setPixel( QPoint( image.width()-1, iy ), Qt::color0 ); }

      painter->drawImage( image.rect(), image );
      //painter->drawPixmap( QPoint( 0, 0 ), QBitmap::fromImage( image ) );

      }

      };

      int main( int argc, char** argv )
      {
      QApplication app( argc, argv );
      QGraphicsScene* scene( new QGraphicsScene );
      scene->setSceneRect( QRectF( 0, 0, 450, 450 ) );

      MyImageItem imgItem;
      imgItem.setPos( 50, 50 );

      scene->addItem( &imgItem );

      // view
      MyGraphicsView view( scene );
      view.resize( 500, 500 );
      view.show();

      return app.exec();

      }

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

            sletta Gunnar Sletta
            janichol Andy Nichols
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes