Qt:调整包含QPixmap的QLabel的大小,同时保持其宽高比 [英] Qt: resizing a QLabel containing a QPixmap while keeping its aspect ratio

查看:571
本文介绍了Qt:调整包含QPixmap的QLabel的大小,同时保持其宽高比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用QLabel向用户显示更大,动态变化的QPixmap的内容。根据可用空间的不同,将此标签缩小/放大会很好。屏幕尺寸并不总是和QPixmap一样大。

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.

如何修改 QSizePolicy 和<$ c QLabel的$ c> sizeHint()来调整QPixmap的大小,同时保持原始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?

我无法修改QLabel的 sizeHint(),将 minimumSize()设置为零无济于事。在QLabel上设置 hasScaledContents()允许增长,但实际上会破坏长宽比...

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...

对QLabel进行子类化帮助,但是此解决方案为一个简单的问题添加了过多的代码...

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?

推荐答案

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

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:


  • 更新像素图时

  • 在包含以下内容的小部件的 resizeEvent 中标签

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

这篇关于Qt:调整包含QPixmap的QLabel的大小,同时保持其宽高比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆