当前位置:网站首页>Qt开发高级进阶:如何在显示时适合视窗宽度和高度(fitWidth+fitHeight)
Qt开发高级进阶:如何在显示时适合视窗宽度和高度(fitWidth+fitHeight)
2022-07-26 14:40:00 【高精度计算机视觉】
Qt调整图片的显示高宽做得十分人性化,只需要在scene和view中支持鼠标滚动轮即可。不过有时候有一些特殊的要求,比如,如何在视窗内实现适合视察的宽度和高度。
注意:这里是指调整图片的显示大小,例如一张800x600的图片,图片大小是不变的,只是显示成任意大小,Qt提供了一个函数scale,同时,配套使用的是transform,算法的基本原理是这样的,
class GraphViewer : public QGraphicsView
{
....
}
int GraphViewer::fitWidth()
{
QTransform tf = transform();
qreal rx = tf.m11(); // this is X scale
//qreal ry = tf.m22();
int vw = rect().width();
int sw = scene()->sceneRect().width();
qreal rw = (qreal)vw/sw;
if( (0==rx) || (abs(rw-rx)< 0.000001) ) {
return 0;
}
qreal ratio = rw /rx;
scale(ratio, ratio);
return 0;
}这里,scale中的两人个参数表示x, y 轴的显示比例,transform中的m11, m22则分别对应这两个比例。
这里特别注意的是,
qreal ratio = rw /rx;如果我们使用
qreal ratio = rw;那么在rw不为1.0的情况下,多执行几次fitWidth,图片会越来越小,或越来越大;因为scale是累积相乘。所以正确的做法是,前一次的视图比例乘 1/rx 后,相当于该维度上scale会被置为1.0,然后,再乘以我们想要的rw(适合宽度)的视图比,就能使图片正好适合视察宽度了。
本文结束。
边栏推荐
- RPN:Region Proposal Networks (区域候选网络)
- 下一代视觉Transformer:解锁CNN和Transformer正确结合方法
- Would you please refer to the document of Database specification?
- 基于CAS的SSO单点客户端配置
- CAS single sign on
- SiamRPN++:深层网络连体视觉跟踪的演变
- [1.2. return and risk of investment]
- OpenCV中图像算术操作与逻辑操作
- Lean product development: principles, methods and Implementation
- Wechat applet - "do you really understand the use of applet components?
猜你喜欢

Arithmetic operation and logic operation of image in opencv

C语言入门必刷100题合集之每日一题(1-20)

Win11运行虚拟机死机了?Win11运行VMware虚拟机崩溃的解决方法

下一代视觉Transformer:解锁CNN和Transformer正确结合方法
![[file upload vulnerability-06] distributed configuration file attack experiment - take upload-labs-4 as an example](/img/47/0e2a159bbbc7af8ef6263ca4ccc724.png)
[file upload vulnerability-06] distributed configuration file attack experiment - take upload-labs-4 as an example

jmeter分布式

Keyboard shortcut to operate the computer (I won't encounter it myself)

【2022国赛模拟】白楼剑——SAM、回滚莫队、二次离线

Minecraft 1.16.5模组开发(五十二) 修改原版生物战利品 (Loot Table)

Fill in the questionnaire and receive the prize | we sincerely invite you to fill in the Google play academy activity survey questionnaire
随机推荐
CVE-2022-33891 Apache spark shell 命令注入漏洞复现
一个满的10L容器,7L、4L空的容器,如何得到5L的水
[2022 national game simulation] Bai Loujian - Sam, rollback Mo team, second offline
【2022国赛模拟】白楼剑——SAM、回滚莫队、二次离线
图神经网络Core数据集介绍
JS analog clock with text label
Arithmetic operation and logic operation of image in opencv
什么是传输层协议TCP/UDP???
《MySQL高级篇》五、InnoDB数据存储结构
Siamfc: full convolution twin network for target tracking
Siamrpn++: evolution of deep network connected visual tracking
NLP之NER:商品标题属性识别探索与实践
CAS based SSO single point client configuration
填问卷,领奖品 | 诚邀您填写 Google Play Academy 活动调研问卷
CAS based SSO single point server configuration
AMB | towards sustainable agriculture: rhizosphere microbial engineering
基于CAS的SSO单点服务端配置
Figure introduction to neural network core dataset
Error reported by Nacos enabled client
SSH that must be read on cloud native