我正在使用 Qt/C++ 编写一个日程安排类型的应用程序,希望在窗口的一部分显示每周的日程安排,并随着窗口大小的增加而呈现这种比例。渲染将由其中包含文本的矩形组成,随着显示区域的增加,矩形应该很好地缩放,而文本应该保持相同的大小。
我试验过 QGraphicsScene 和 QGraphicsView,我可以制作矩形和文本比例;然而,矩形缩放看起来很难看(拉伸(stretch)轮廓),我根本不想缩放文本。
我怀疑我可能想将场景调整到显示区域并重新绘制矩形和文本;但是,我不确定该怎么做——QGraphicsScene 似乎没有响应 resizeEvent。这是正确的方法吗?
最佳答案
我不确定丑陋的矩形缩放是关于什么的(截图可能会帮助我更好地理解你的意思),但如果你不想缩放文本部分,你可以通过调用 setFlag( ItemIgnoresTransformations, true) 在你的 QTextGraphicItem 对象上。
就自动重新缩放矩形以响应窗口大小调整而言,您可以查看 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. 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. You can toggle the scrollbar policies to always on or always off to prevent this (see horizontalScrollBarPolicy() and verticalScrollBarPolicy()).
关于c++ - 在 Qt 中缩放图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20648430/