Results 1 to 4 of 4

Thread: QImage and drawImage method

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

    Default QImage and drawImage method

    uso il thread per chiedere un informazione simile : ho un QFrame su cui ho necessità di visualizzare e nascondere 10 immagini fisse. E' corretto caricarle in un metodo di init tramite un metodo simile :

    Hi, I have a question about best way to use painter.drawImage : I need to show and hide 10 images in a QFrame. I decided to init them using pointer QImage in a init() method, then when needed show them by paint call. Is it a good way or maybe can be memory problems?

    void MiaClasse::init(){

    image1 = new QImage;
    image1->load("resources/images/image1.png");

    }

    void MiaClasse:aintEvent(QPaintEvent *)
    {
    QPainter painter(this);
    painter.drawImage(155, 130, *image1);
    }

    thank you

  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

    Your solution works--for displaying one image anyway--, but you could make a few improvements.

    Firstly, your code leaks memory because the QImage is never deallocated. You could replace image1 with a smart pointer (such as std::unique_ptr), or even not go through a pointer at all: let image1 be a QImage. It will be allocated/deallocated with the containing MiaClasse instance.

    Secondly, you had the right intuition by preloading the image instead of loading it from disk in paintEvent(), but you could do even better by using QPixmap instead of QImage. The former is more readily usable by a painter.

  3. #3
    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 

  4. #4
    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, 09:14
  2. Replies: 5
    Last Post: 13th November 2013, 02:54
  3. Factoring drawImage
    By Alundra in forum Qt Programming
    Replies: 0
    Last Post: 6th November 2013, 12:39
  4. QPainter.drawImage() not working with resource image file
    By thiagoalencar22 in forum Qt Programming
    Replies: 4
    Last Post: 22nd April 2010, 22:07
  5. QImage : trouble with save() method
    By krivenok in forum Qt Programming
    Replies: 12
    Last Post: 3rd May 2006, 21: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.