在Qt中显示图像以适合标签尺寸 [英] Display image in Qt to fit label size

查看:456
本文介绍了在Qt中显示图像以适合标签尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了几种在表单上显示图像的方法,但它们都没有按照我的意愿运行。

I already tried several methods on displaying an image on a form, but none of them works how I would like.

我读过许多地方,最简单的方法是创建标签并用它来显示图像。我有一个标签,其大小由布局指定,但如果我用像素图加载图像,标签将调整大小为图像的大小。如果我使用img标签作为text或css背景属性,它将不会显示整个图像。我想要做的是加载图像并装入标签,而不是更改标签的大小,但是当我调整窗口大小时,以及调整标签大小时,图像也应该调整大小以便它始终适合进入它。

I've read many places that the easiest way is to create a label and use that to display the image. I have a label, which size is specified by the layout, but if I load an image into it with a pixmap, the label is resized to the size of the image. If I use img tag as text or css background property, it won't display the whole image. What I would like to do is to load the image and fit into the label, not changing the label's size, but when I resize my window, and by that resizing the label as well, the image should be resized too so it will always fit into it.

如果唯一的方法是获得标签的大小,并调整像素图以使其适合,并处理调整大小事件(信号),我怎么能调整像素图的大小?我希望我不需要将整个事物保存到QImage中并且每次都从它创建一个像素图。

If the only method is to get the label's size, and resize the pixmap so it would fit, and handle the resize event (signal), how could I resize the pixmap? I hope I won't need to save the whole thing into a QImage and create a pixmap from it each time.

另外,我该如何居中?如果它不能同时适合宽度和高度,我希望较小的尺寸居中。

Also, how can I center it? If it can't fit both the width and the height, I would like the smaller dimension to be centered.

哦,我不想使用滑块来处理溢出。

Oh, and I don't want to use sliders to handle overflows.

推荐答案

QLabel :: setScaledContents(bool)有帮助吗? 图像查看器示例也是。

这篇关于在Qt中显示图像以适合标签尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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