-
Bug
-
Resolution: Done
-
P2: Important
-
4.5.0
-
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++ )
QAction* action = menu.exec( event->globalPos() );
if ( action )
}
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() )
} 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 ) )
}
} 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();
}