Qt:调整QLabel的大小,包含一个QPixmap,同时保持它的宽高比(Qt: resizing a QLabel containing a QPixmap while keeping its as

编程入门 行业动态 更新时间:2024-10-23 03:15:16
Qt:调整QLabel的大小,包含一个QPixmap,同时保持它的宽高比(Qt: resizing a QLabel containing a QPixmap while keeping its aspect ratio)

我使用QLabel向用户显示更大,动态变化的QPixmap的内容。 根据可用的空间使这个标签更小/更大是很好的。 屏幕截图并不总是像QPixmap一样大。

如何修改QLabel的QSizePolicy和sizeHint()来调整QPixmap的大小,同时保持原始QPixmap的宽高比?

我不能修改QLabel的sizeHint() ,设置minimumSize()所以零没有帮助。 设置"hasScaledContents()"的QLabel允许增长,但打破纵横比的东西...

子类化QLabel确实有帮助,但这个解决方案增加了很多代码只是一个简单的问题...

任何聪明的提示如何实现这个没有子类化? 非常感谢!

I use a QLabel to display the content of a bigger, dynamically changing QPixmap to the user. It would be nice to make this label smaller/larger depending on the space available. The screen size is not always as big as the QPixmap.

How can I modify the QSizePolicy and sizeHint() of the QLabel to resize the QPixmap while keeping the aspect ratio of the original QPixmap?

I can't modify sizeHint() of the QLabel, setting the minimumSize() to zero does not help. Setting hasScaledContents() on the QLabel allows growing, but breaks the aspect ratio thingy...

Subclassing QLabel did help, but this solution adds too much code for just a simple problem...

Any smart hints how to accomplish this without subclassing?

最满意答案

为了更改标签尺寸,您可以为标签选择适当的大小策略,例如展开或最小展开。

您可以通过在每次更改时保持其纵横比来缩放像素图:

QPixmap p; // load pixmap // get label dimensions int w = label->width(); int h = label->height(); // set a scaled pixmap to a w x h window keeping its aspect ratio label->setPixmap(p.scaled(w,h,Qt::KeepAspectRatio));

有两个地方你应该添加这个代码:

当pixmap更新时 在包含标签的小部件的resizeEvent中

In order to change the label size you can select an appropriate size policy for the label like expanding or minimum expanding.

You can scale the pixmap by keeping its aspect ratio every time it changes:

QPixmap p; // load pixmap // get label dimensions int w = label->width(); int h = label->height(); // set a scaled pixmap to a w x h window keeping its aspect ratio label->setPixmap(p.scaled(w,h,Qt::KeepAspectRatio));

There are two places where you should add this code:

When the pixmap is updated In the resizeEvent of the widget that contains the label

更多推荐

本文发布于:2023-07-29 20:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319398.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:大小   QPixmap   Qt   QLabel   aspect

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!