Results 1 to 6 of 6

Thread: How to solve this problem about drawImage() and paintEvent()?

  1. #1
    Join Date
    Jun 2013
    Posts
    56
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default How to solve this problem about drawImage() and paintEvent()?

    Hi, all
    I am working under linux with QT4.7, and my test code is like below:
    Qt Code:
    1. 12 void MyWidget :: paintEvent ( QPaintEvent * event )
    2. 13 {
    3. 14 init();
    4. 15 }
    5. 16
    6. 17 void MyWidget :: init()
    7. 18 {
    8. 19 QImage bg( width(), height(), QImage::Format_ARGB32_Premultiplied );
    9. 20 QImage a( width(), height(), QImage::Format_ARGB32_Premultiplied );
    10. 21 QImage b( width(), height(), QImage::Format_ARGB32_Premultiplied );
    11. 22
    12. 23 QPainter p;
    13. 24
    14. 25 p.begin( &bg );
    15. 26 QRect r = rect();
    16. 27 p.fillRect( r, Qt::white );
    17. 28 p.end();
    18. 29
    19. 30 p.begin( &a );
    20. 31 p.setPen( QPen( QColor( Qt::red ), 5 ) );
    21. 32 p.drawLine( 10, 10, 50, 50 );
    22. 33 p.end();
    23. 34
    24. 35 p.begin( &b );
    25. 36 p.setPen( QPen( QColor( Qt::green ), 5 ) );
    26. 37 p.drawLine( 10, 70, 50, 10 );
    27. 38 p.end();
    28. 39
    29. 40 p.begin( this );
    30. 41 p.drawImage( QPoint(0,0), bg );
    31. 42 p.drawImage( QPoint(0,0), a );
    32. 43 p.drawImage( QPoint(0,0), b );
    33. 44 p.end();
    34. 45 }
    To copy to clipboard, switch view to plain text mode 

    Then, at the beginning the widget is shown correctly - there are two lines cross with each other at the Top-Left corner, one is green and another is red. But when I drag the Bottom-Right corner of this widget to resize it, the two lines will be repainted in a wrong way. Why and how to solve it?

    Any suggestion is appreciated.

    Nicho
    Last edited by Nicho; 12th November 2013 at 06:16.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to solve this problem about drawImage() and paintEvent()?

    How are they painted incorrectly?
    I.e. what are you expecting to see?

    Btw, you can draw directly onto the widget in paintEvent(), i.e. create a QPainter on "this";

    Cheers,
    _

  3. #3
    Join Date
    Jun 2013
    Posts
    56
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to solve this problem about drawImage() and paintEvent()?

    Quote Originally Posted by anda_skoa View Post
    How are they painted incorrectly?
    I.e. what are you expecting to see?

    Btw, you can draw directly onto the widget in paintEvent(), i.e. create a QPainter on "this";

    Cheers,
    _
    anda_skoa,
    I do not know why, but I failed to upload the pictures which may express my problem more clearly. so, would you do me a favour?Just use my code and show MyWidget directly in the main function, and try to resize the widget by mouse. If you see the two lines not being repained as they are defined, that is my problem. Thanks so much.

    To paint with drawImage is because I want to do some test about image layer management.

    Nicho

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to solve this problem about drawImage() and paintEvent()?

    Quote Originally Posted by Nicho View Post
    Just use my code and show MyWidget directly in the main function, and try to resize the widget by mouse. If you see the two lines not being repained as they are defined, that is my problem. Thanks so much.
    Maybe you should provide the code then. At least something that can be copied into an editor. You current code is incomplete and has "line numbers" mixed with the actual code.

    Cheers,
    _

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to solve this problem about drawImage() and paintEvent()?

    Here is a complete example:
    Qt Code:
    1. #include <QApplication>
    2. #include <QWidget>
    3. #include <QImage>
    4. #include <QPainter>
    5.  
    6. class MyWidget: public QWidget {
    7. Q_OBJECT
    8. public:
    9. MyWidget(QWidget *p = 0): QWidget(p) {
    10. }
    11. protected:
    12. void paintEvent ( QPaintEvent * event );
    13. private:
    14. void init();
    15. };
    16.  
    17. void MyWidget :: paintEvent ( QPaintEvent * event )
    18. {
    19. init();
    20. }
    21.  
    22. void MyWidget :: init()
    23. {
    24. QImage bg( width(), height(), QImage::Format_ARGB32_Premultiplied );
    25. QImage a( width(), height(), QImage::Format_ARGB32_Premultiplied );
    26. QImage b( width(), height(), QImage::Format_ARGB32_Premultiplied );
    27.  
    28.  
    29. p.begin( &bg );
    30. QRect r = rect();
    31. p.fillRect( r, Qt::white );
    32. p.end();
    33.  
    34. p.begin( &a );
    35. p.setPen( QPen( QColor( Qt::red ), 5 ) );
    36. p.drawLine( 10, 10, 50, 50 );
    37. p.end();
    38.  
    39. p.begin( &b );
    40. p.setPen( QPen( QColor( Qt::green ), 5 ) );
    41. p.drawLine( 10, 70, 50, 10 );
    42. p.end();
    43.  
    44. p.begin( this );
    45. p.drawImage( QPoint(0,0), bg );
    46. p.drawImage( QPoint(0,0), a );
    47. p.drawImage( QPoint(0,0), b );
    48. p.end();
    49. }
    50.  
    51. int main(int argc, char **argv)
    52. {
    53. QApplication app(argc, argv);
    54. MyWidget w;
    55. w.show();
    56. return app.exec();
    57. }
    58. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    The widget does indeed get garbled as you resize the window. When you create a QImage as you have done it is not initialised (as clearly stated in the docs). You fill the background image with white but leave the others with whatever random rubbish they started with. On top of the random rubbish you draw a line, then draw the combined result over the white background image. It's dumb luck if the images start out clear of rubbish (possibly because the memory is zeroed when first allocated to the process by the OS). In my case, each time through the paintEvent() the images are put in slightly different memory locations that overlapped their previous position so recognisable ghosts of former images are visible.

    Does seem a long-winded way to do this:
    Qt Code:
    1. class TypicalWidget: public QWidget {
    2. Q_OBJECT
    3. public:
    4. TypicalWidget(QWidget *p = 0): QWidget(p) {
    5. }
    6. protected:
    7. void paintEvent ( QPaintEvent * event ) {
    8. QPainter p(this);
    9. p.fillRect(rect(), Qt::white);
    10. p.setPen( QPen( QColor( Qt::red ), 5 ) );
    11. p.drawLine( 10, 10, 50, 50 );
    12. p.setPen( QPen( QColor( Qt::green ), 5 ) );
    13. p.drawLine( 10, 70, 50, 10 );
    14. }
    15. };
    To copy to clipboard, switch view to plain text mode 
    but you might have a reason.

  6. #6
    Join Date
    Jun 2013
    Posts
    56
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to solve this problem about drawImage() and paintEvent()?

    ChrisW67,
    Thank you for your reply in detail.
    I guessed the QImage would be initialized as transparency, so I leaved them as not filled.
    I have re-read the docs, and here is the important information :
    Warning: This will create a QImage with uninitialized data. Call fill() to fill the image with an appropriate pixel value before drawing onto it with QPainter.
    I think I got what I should do now.
    Thank you again.

    Nicho

Similar Threads

  1. How to solve GUI freezing problem
    By honestapple in forum Newbie
    Replies: 28
    Last Post: 6th April 2013, 12:35
  2. Replies: 1
    Last Post: 20th July 2012, 16:38
  3. vary hard problem to solve
    By FS Lover in forum Newbie
    Replies: 3
    Last Post: 2nd July 2009, 09:26
  4. How to solve this cc1plus: out of memory problem?
    By triperzonak in forum Qt Programming
    Replies: 2
    Last Post: 28th September 2008, 20:20
  5. Replies: 12
    Last Post: 3rd April 2006, 06:11

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.