QPixmap loadFromData function used some strange mechanism for cache?

There is a strange phenomenon in the QPixmap loadFromData function. This is true:

QPixmap pix1(":/test1.png");    
QPixmap pix2(":/test2.png");
ui->labelPix1>setPixmap(pix1);
pix1 = pix2;
ui->labelPix2->setPixmap(pix1);

      

In the above code, the tow mark controls loaded the same pix1 QPixmap object, but pix1 was modified before labelPix2 loaded it, so the tow marks show different images (test1.png and test2.png), that's correct.

Then we change the line "pix1 = pix2", use the following block instead:

QByteArray ba;
QBuffer buf(&ba);
pix2.save(&buf, "PNG");
pix1.loadFromData(ba);

      

In this block, pix1 has loaded byte data from pix2 after it has set labelPix1. Obviously labelPix2 shows the "test2.png" image, but strangely labelPix1 also shows the "test2.png" image (they show the same image).

In addition, we present a third QPixmap, pix3, and start with "test1.png" just like pix1. This time labelPix2 does not set pix1, but sets pix3 after pix1 loads byte data from pix2. Similar:

QPixmap pix1(":/test1.png");    
QPixmap pix2(":/test2.png");
QPixmap pix3(":/test1.png");
ui->labelPix1>setPixmap(pix1);

QByteArray ba;
QBuffer buf(&ba);
pix2.save(&buf, "PNG");
pix1.loadFromData(ba);

ui->labelPix2->setPixmap(pix3);

      

Oh, labelPix1 and labelPix3 show the same "test2.png" image.

I think there must be some kind of mechanism. But I don't know what it really is. Does any organ have an idea?

+3


source to share


1 answer


This is due to implicit exchange . I don't know if this is intentional or not. But if you look QPixmap::load

, you will notice what it is calling QPixmap::detach

. This means that it will no longer work with shared data. And the documents say A pixmap is automatically detached by Qt whenever its contents are about to change.

.



But now when you look QPixmap::loadFromData

, you will notice that it is not calling QPixmap::detach

. Now that I've quoted the docs before, QPixmap::detach

Qt should be automatically called when its contents are about to change. So why not in this case? I do not know this.

+3


source







All Articles