lni
27th March 2009, 00:28
Hi,
Can someone please verify this? Click the 3rd button on the viewer, and print to a file, it appears to be a bug.
#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 );
// 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();
}
Can someone please verify this? Click the 3rd button on the viewer, and print to a file, it appears to be a bug.
#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 );
// 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();
}