On QLabel the image slides after setmask
-
Hi Guys!
I tried to create a QLabel with a png with alpha channels like this:QLabel *labi = new QLabel; labi->setPixmap(QPixmap("madi.png"));
After that, I set the image mask for it because I want the edges of the QLabel to match the edges of the image:
labi->setMask(labi->pixmap().scaled(labi->pixmap().width(),labi->pixmap().height(),Qt::KeepAspectRatio,Qt::SmoothTransformation).mask());
I get approximately good results, BUT the image slides on it:
What is the correct way to solve this? And where are my mistakes? Sorry if its a really lame question. Thank you in advance for your help!
-
@Kaguro If your label still has a fixed size, then just modify your code a little:
labi->setScaledContents(true); //...set fixed size... labi->setMask(labi->pixmap().scaled(labi->width(), labi->height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation).mask());
In your original code the scaling doesn't work because
labi->pixmap()
always return the original pixmap, not the scaled one, so the size it returns is not the actually showing size. And also whenQLabel
scales it ignores aspect ratio.If you want the mask can change dynamically with the label size, then it is too complicated.
You'll need to subclassQLabel
and create a scaled pixmap wheneven its size changed and reset the mask. -
@Kaguro
This is a total guess, may be nonsense! If you havelabi->setMask(labi->pixmap().scaled(labi->pixmap().width(),labi->pixmap().height()
does that mask have to match the scaling of the original pixmap? Does your label needsetScaledContents(true)
to match that, or have itspixmap()
scaled to fit? Or just ignore this :) -
@JonB Yes I wanted to solve with that part that mask can scaling of the original pixmap. I think its pixmap() scaled to fit. I tried with this now:
QLabel *labi = new QLabel; labi->setScaledContents(true); labi->setPixmap(QPixmap("madi.png")); labi->setMask(labi->pixmap().mask());
And the result:
-
@Kaguro If your label still has a fixed size, then just modify your code a little:
labi->setScaledContents(true); //...set fixed size... labi->setMask(labi->pixmap().scaled(labi->width(), labi->height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation).mask());
In your original code the scaling doesn't work because
labi->pixmap()
always return the original pixmap, not the scaled one, so the size it returns is not the actually showing size. And also whenQLabel
scales it ignores aspect ratio.If you want the mask can change dynamically with the label size, then it is too complicated.
You'll need to subclassQLabel
and create a scaled pixmap wheneven its size changed and reset the mask. -
K Kaguro has marked this topic as solved on