[Go]-qpixmap global variable loading multiple pictures failure problem

Source: Internet
Author: User

I think Qt qpixmap This class is very familiar to everyone, it can be very simple to map on the label, for example:

Qpixmap p;

P.load ("1.png");

Label->setpixmap (P);

It's that simple, but I suddenly find a problem today:

Qpixmap p;

P.load ("1.png");

Label->setpixmap (P);

P.load ("2.png");

Label->setpixmap (P);

P.load ("1.png");

Label->setpixmap (P);

When using the same p, which is the global variable, you load image 1, display picture 1; Reload image 2, display picture 2; But when you want to change back to the picture, you load the picture one, do not make it, do not come back, or show the picture two, you load a picture three, so that you want to change back to the picture two, not so ...

This is for God horse???

Originally, P.load () is to load the picture into the buffer: Qpixmapcache, when you load the picture two is, the picture is not overwritten, at this time the buffer has picture one and picture two, when you load the picture again, thought the buffer should have, so p.load (), Returns true directly, but the picture is still picture two, so it cannot be displayed back to the picture One!

The solution is simple and there are three ways to do it:

First, the direct definition of local variables, so that each time is a new buffer;

Second, when the picture is loaded again, the buffer is emptied first, qpixmapcache::clear (), and then loaded;

Three, in the initialization function write: Qpixmapcache::setcachelimit (1); Set the buffer can only put a picture, so that will replace the original picture, you can load new pictures!

[Go]-qpixmap global variable loading multiple pictures failure problem

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.