Qt:调整QLabel的大小,包含QPixmap,同时保持其纵横比 [英] Qt: resizing a QLabel, containing a QPixmap, while keeping it's aspect ratio

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

问题描述

我使用QLabel向用户显示更大,动态变化的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 Screensize is not always as big as the QPixmap.

如何修改 QSizePolicy 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?

code> sizeHint()的QLabel,设置 minimumSize()所以零没有帮助。设置hasScaledContents()的QLabel允许增长,但打破长宽比的东西...

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

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

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

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

Any smart hints how to accompllish this without subclassing? Thanks a lot!

推荐答案

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

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:调整QLabel的大小,包含QPixmap,同时保持其纵横比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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