Maintaining aspect ratio of qlabel
-
Hello,
I am using the following example to implement QLabel
https://stackoverflow.com/questions/42833511/qt-how-to-create-image-that-scale-with-window-and-keeps-aspect-ratio
Problem:
When I write the following syntax:
_qPixmapScaled = _qPixmap.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
I get a complete view of the pixmap image, although it does not cover the complete label widgets, i.e., there is space on the corners.
When I use Qt:: KeepAspectRatiobyExpanding, the pixmap occupies the complete label, but I do not get the full view of the pixmap ie, some part gets truncated. I have to resize the Qlabel to view the image.
Requirement:
I need to adjust the size of the label, keeping the complete view of the image. How can i achieve the following? -
Hi
Just as a note.
Keeping the aspect ratio of an image often leaves
unused area when the numbers do not match.
So its expected if you want to keep the aspect. -
@mrjj : thanks for the reply.
Yes, I want to keep the aspect ratio.
What are the possible solutions or workaround if I don't want the unused space.
As I said earlier, Qt:: KeepAspectRatiobyExpanding often covers the unused area, but it does not display the whole pixmap, some of the regions get truncated. -
Well you can either accept that for some sizes there are unused space but
image keeps its aspect or you can
allow it to not keep aspect and complete fill the eare even if it slightly distorts the image.The KeepAspectRatiobyExpanding will make sure image uses all space but
some of the image might be out side the area.SO you can either have it take all space or have it keep the aspect ratio.
Its not possible to have both in the cases where the numbers do not match up.
-
@Kira said in Maintaining aspect ratio of qlabel:
What do exactly mean for numbers do not match up?
if height != width
-
you can override paintEvent and do it yourself:
for example (assuming its a square image)
void myLabel::paintEvent( QPaintEvent* event ) { int min = qMin(height(), width()); int xOff = (width() - min) / 2; int yOff = (height() - min) / 2; QPainter p(this); p.drawImage(QRect(xOff,yOff,min,min), m_myImage); }
-
@J-Hilk : Thanks for the reply:
Current I am using the following:void LabelAnnotation::resizeEvent(QResizeEvent *pQEvent) { QLabel::resizeEvent(pQEvent); setPixmap(_qPixmap, pQEvent->size()); } void LabelAnnotation::setPixmap(const QPixmap &qPixmap, const QSize &size) { QPixmap _qPixmapScaled; _qPixmap = qPixmap; _qPixmapScaled = _qPixmap.scaled(size, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation); QLabel::setPixmap(_qPixmapScaled); }
Will I have to remove the above function if i override paintEvent as mentioned ?
-
@Kira I don't think so,
you can access your modified/scaled pixmap by calling
pixmap()
and QPainter has a drawPixmap method as well.
just noticed, that what I wrote, does not actually match what you want.
Do I understand it correctly, that you want a way to rescale your QLabel, so that it fits the Pixmap ?
-
@Kira
Ok, I suggest the following, I used it before with moderate success rate.set the SizePolcy of either width or height to fixed.
inside the overloaded resizeEvent, set the min width/height of your fixed side to the appropriate length (according to the pixmap ratio)
It should work, but be beware of potential recursive resizeEvents, as this is a clunky workaround.
-
What does moderate success mean
when actively resizing the window, you may get "flickering" as the new min size may tricker a new recalculating of the other widgets inside the layout
How can I determine the aspect ratio
I will figure the code but would be grateful if you have any working sampleuntested:
//Assuming horizontal size policy is fixed void LabelAnnotation::resizeEvent(QResizeEvent *pQEvent) { double minw = (static_cast<double>(_qPixmap.width()) * height()) / _qPixmap.height(); setMinimumWidth(static_cast<int>(minw)); }
-
@Kira said in Maintaining aspect ratio of qlabel:
I implemented the following
I don't see it. May be lost in the depth of the forum ?
-
@J-Hilk said in Maintaining aspect ratio of qlabel:
void LabelAnnotation::resizeEvent(QResizeEvent *pQEvent)
{
double minw = (static_cast<double>(_qPixmap.width()) * height()) / _qPixmap.height();
setMinimumWidth(static_cast<int>(minw));
}This one :)