Results 1 to 7 of 7

Thread: QPainter bug or QPrinter bug?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QPainter bug or QPrinter bug?

    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 

  2. #2
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QPainter bug or QPrinter bug?

    And what should happen? I can see 4 squares and i can right click on them and save them to file (see attachment). And the file looks exactly like the window content...
    Attached Images Attached Images
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  3. #3
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QPainter bug or QPrinter bug?

    Saving to image is fine. But when you print to a pdf file, it is bad...

    Try this new example, it looks worse with text in my computer:
    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. QFont font = painter->font();
    82. font.setPointSize( 16 );
    83.  
    84. painter->setFont( font );
    85. painter->drawText( 30, 30, "Text behind a transparent image" );
    86.  
    87. // create a mono image
    88. QImage image( boundingRect().size().toSize(), QImage::Format_Mono );
    89.  
    90. // set color1 to be transparent and fill the image
    91. image.setColor( Qt::color1, Qt::transparent );
    92. image.fill( Qt::color1 );
    93.  
    94. // set foreground color
    95. image.setColor( Qt::color0, pen.color().rgb() );
    96.  
    97. for ( int ix = 0; ix < image.width(); ix++ ) {
    98. image.setPixel( QPoint( ix, 1 ), Qt::color0 );
    99. image.setPixel( QPoint( ix, image.height()/2 ), Qt::color0 );
    100. image.setPixel( QPoint( ix, image.height() - 1 ), Qt::color0 );
    101. }
    102.  
    103. for ( int iy = 0; iy < image.height(); iy++ ) {
    104. image.setPixel( QPoint( 1, iy ), Qt::color0 );
    105. image.setPixel( QPoint( image.width()/2, iy ), Qt::color0 );
    106. image.setPixel( QPoint( image.width()-1, iy ), Qt::color0 );
    107. }
    108.  
    109. painter->drawImage( image.rect(), image );
    110. //painter->drawPixmap( QPoint( 0, 0 ), QBitmap::fromImage( image ) );
    111.  
    112. }
    113.  
    114. };
    115.  
    116. int main( int argc, char** argv )
    117. {
    118. QApplication app( argc, argv );
    119. scene->setSceneRect( QRectF( 0, 0, 450, 450 ) );
    120.  
    121. MyImageItem imgItem;
    122. imgItem.setPos( 50, 50 );
    123.  
    124. scene->addItem( &imgItem );
    125.  
    126. // view
    127. MyGraphicsView view( scene );
    128. view.resize( 500, 500 );
    129. view.show();
    130.  
    131. return app.exec();
    132.  
    133. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QPainter bug or QPrinter bug?

    What exactly is the problem?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QPainter bug or QPrinter bug?

    I attached 2 snapshots, 1st one is the plot by the program, 2nd one is a pdf output. It appears the pdf engine or QPrinter can't handle QImage::Format_Mono image that has transparent background... Also, the text is wrong too.
    Attached Images Attached Images

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QPainter bug or QPrinter bug?

    I think the general problem is that you don't into account that the screen and the pdf have different resolutions. I've been printing using Qt's PDF generator and I can assure you it works just fine.

    As for the image, I'd say the Mono format gets stored as an image that doesn't support transparency (JPG maybe?) or it gets converted to such a format. Just to be sure it's not the problem with the pdf renderer in kpdf, check the file with some other viewer (okular or acroread).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    faldzip (30th March 2009)

  8. #7
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QPainter bug or QPrinter bug?

    I have been trying in many different machines with different kernels. In Windows, I tried CutePDF writer (http://www.cutepdf.com/), as well as Adobe's pdf writer. In linux, I have tested on Fedora core 4 and core 10, as well as enterprise Linux 4.0. For pdf viewer, I have tried acrobat in windows. In Linux, I tried kpdf, evince, and okular. All of them give the same error result.

    However, if I switch to QImage::Format_Indexed8, it works fine. But QImage::Format_Indexed8 is really a waste of resources. All I need is two colors in the image...

    I don't know why the font is distorted by painter...

    I just reported this bug to Cattell a few weeks ago:
    http://www.qtsoftware.com/developer/...9&method=entry

Similar Threads

  1. QPrinter, QPainter, and Large Amounts of Data
    By millsks in forum Qt Programming
    Replies: 0
    Last Post: 17th March 2009, 18:26
  2. Replies: 5
    Last Post: 15th January 2009, 09:03
  3. QPrinter problems
    By Teuniz in forum Qt Programming
    Replies: 1
    Last Post: 3rd November 2007, 08:51
  4. QPainter & QPrinter on linux (fedora 7)
    By wbt_ph in forum Qt Programming
    Replies: 4
    Last Post: 20th October 2007, 15:37
  5. QPainter & QPrinter
    By munna in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2006, 14:19

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.