简体   繁体   English

在Qt中缩放图形

[英]Scaling graphics in Qt

I am writing a scheduling-type application using Qt/C++ and want to display weekly schedules in one part of the window, and have this rendering scale as the window size increases. 我正在使用Qt / C ++编写日程安排类型的应用程序,并希望在窗口的一部分中显示每周的日程安排,并随着窗口大小的增加而具有此呈现比例。 The renders will be composed of rectangles with text in them, and as the display area increases the rectangles should scale nicely while the text should remain the same size. 渲染器将​​由其中带有文本的矩形组成,并且随着显示区域的增加,矩形应很好地缩放,而文本应保持相同的大小。

I have experimented with QGraphicsScene and QGraphicsView and I can make rectangles and text scale; 我已经对QGraphicsScene和QGraphicsView进行了实验,并且可以制作矩形和文本比例尺。 however, the rectangle scaling seems ugly (stretches the outline) and I don't want text to scale at all. 但是,矩形缩放似乎很丑陋(拉伸轮廓),我根本不希望文本缩放。

I suspect that I might want to resize the scene to the display area and re-draw the rectangles and text; 我怀疑我可能想将场景的大小调整到显示区域并重新绘制矩形和文本; however, I am not sure how to do this - QGraphicsScene doesn't seem to respond to resizeEvent. 但是,我不确定如何执行此操作-QGraphicsScene似乎不响应resizeEvent。 Is this even the right approach? 这是正确的方法吗?

I'm not sure what the ugly rectangle scaling is about (a screenshot might help me understand better what you meant there), but if you don't want the text parts to scale, you can accomplish that by calling setFlag(ItemIgnoresTransformations, true) on your QTextGraphicItem objects. 我不确定丑陋的矩形缩放是关于什么的(屏幕截图可以帮助我更好地理解您的意思),但是如果您不希望缩放文本部分,可以通过调用setFlag(ItemIgnoresTransformations,true )放在您的QTextGraphicItem对象上。

As far as automatically rescaling the rectangles in response to a window resize, you might take a look at the documentation of the QGraphicsView::fitInView() method: 至于自动调整矩形以响应窗口调整大小,您可以查看QGraphicsView :: fitInView()方法的文档:

Scales the view matrix and scrolls the scroll bars to ensure that the scene rectangle rect fits inside the viewport [...] It's common to call fitInView() from inside a reimplementation of resizeEvent(), to ensure that the whole scene, or parts of the scene, scales automatically to fit the new size of the viewport as the view is resized. 缩放视图矩阵并滚动滚动条,以确保场景矩形rect可以容纳在视口内。通常在重新实现resizeEvent()的内部调用fitInView(),以确保整个场景或部分场景场景的大小,将在调整视图大小时自动缩放以适合视口的新大小。 Note though, that calling fitInView() from inside resizeEvent() can lead to unwanted resize recursion, if the new transformation toggles the automatic state of the scrollbars. 但是请注意,如果新的转换切换了滚动条的自动状态,则从resizeEvent()内部调用fitInView()可能会导致不必要的调整大小递归。 You can toggle the scrollbar policies to always on or always off to prevent this (see horizontalScrollBarPolicy() and verticalScrollBarPolicy()). 您可以将滚动条策略切换为始终打开或始终关闭以防止出现这种情况(请参见horizo​​ntalScrollBarPolicy()和verticalScrollBarPolicy())。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM