Maintaining aspect ratio of qlabel
-
@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 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 well, is your label part of a layout or do you resize it manually?
because you will have to resize it manually, having it in a layout will make things tricky
-
@J-Hilk : My label is part of the Grid layout.
I have seen various articles related to the issue, but none of them provide an exact solution. Is there a specific way to handle such a scenario.@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.
-
@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.
-
@J-Hilk :
OK, thanks, understood.
What does moderate success mean?
How can I determine the aspect ratio ?
I will figure the code but would be grateful if you have any working sample.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)); }
-
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)); }
-
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)); }
-
@J-Hilk : Here are we resizing the window or the QLabel.
I implemented the following, but still, there is space left when I make the window to full screen.@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 ?
-
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)); }
@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 :)
-
can you show us a screenshot of the image inside the layout, it's still difficult for me to visualize what the issue is exactly ;)
-
can you show us a screenshot of the image inside the layout, it's still difficult for me to visualize what the issue is exactly ;)