Results 1 to 4 of 4

Thread: QImage and drawImage method

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2014
    Posts
    6
    Qt products
    Qt4

    Default Re: QImage and drawImage method

    thank you, but init method is called one time only when application start so memory for any image is allocated only one time. Isn't it?
    I also need to make a couple of them blinking so I prefer to load images only one time and paint when needed :

    Qt Code:
    1. void MiaClasse::init(){
    2.  
    3. image1 = new QImage;
    4. image1->load("resources/images/image1.png");
    5.  
    6. image2 = new QImage;
    7. image2->load("resources/images/image2.png");
    8.  
    9. blink = false;
    10.  
    11. }
    12.  
    13. void MiaClasse::letItBlink(){
    14.  
    15. blink = !blink;
    16. repaint();
    17.  
    18. }
    19.  
    20. void MiaClasse:: paintEvent(QPaintEvent *)
    21. {
    22. QPainter painter(this);
    23.  
    24. painter.drawImage(155, 130, *image1);
    25.  
    26. if(blink)
    27. painter.drawImage(155, 130, *image2);
    28. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QImage and drawImage method

    You current design works; loading the images once, upon initialization, instead of every time the widget is painted is the right way. I was not suggesting changing that, quite the contrary.

    Instead of allocating QImages on the heap, you could allocate QPixmaps directly with the MiaClasse:

    Qt Code:
    1. class MiaClasse /* ... */ {
    2. /* ... */
    3. private:
    4. QPixmap image1;
    5. QPixmap image2;
    6. }
    7.  
    8. void MiaClasse::init(){
    9. image1.load("resources/images/image1.png");
    10. image2.load("resources/images/image2.png");
    11. blink = false;
    12. }
    13.  
    14. void MiaClasse::letItBlink(){
    15. blink = !blink;
    16. update(); // Call this instead of QWidget::repaint()
    17. }
    18.  
    19. void MiaClasse:: paintEvent(QPaintEvent *) {
    20. QPainter painter(this);
    21. painter.drawPixmap(155, 130, image1);
    22. if(blink)
    23. painter.drawPixmap(155, 130, image2);
    24. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QPainter::drawImage randomly(?) omits some images
    By Al_ in forum Qt Programming
    Replies: 1
    Last Post: 20th July 2014, 08:14
  2. Replies: 5
    Last Post: 13th November 2013, 01:54
  3. Factoring drawImage
    By Alundra in forum Qt Programming
    Replies: 0
    Last Post: 6th November 2013, 11:39
  4. QPainter.drawImage() not working with resource image file
    By thiagoalencar22 in forum Qt Programming
    Replies: 4
    Last Post: 22nd April 2010, 21:07
  5. QImage : trouble with save() method
    By krivenok in forum Qt Programming
    Replies: 12
    Last Post: 3rd May 2006, 20:55

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.