[英]Painting with rotated QPainter clips to incorrect region of QImage
I have a QImage that represents a blank piece of paper and a QPainter, which is used to paint onto this image. 我有一个QImage代表一张空白的纸和一个QPainter,用于在此图像上绘画。 Sometimes, I will want to rotate/translate the QPainter before any paint operations, in order to paint onto this image in "Landscape" orientation.
有时,我会在进行任何绘制操作之前旋转/平移QPainter,以便以“横向”方向绘制到此图像上。
Here is a simplified snippet of the code: 这是代码的简化片段:
_image = new QImage(paperRect().size(), QImage::Format_RGB888);
_painter->begin(_image);
if (_orientation == QPrinter::Landscape)
{
_painter->translate(0, _image->height());
_painter->rotate(270);
}
// Painting operations here.
Unfortunately, this is not working as I had expected. 不幸的是,这没有按我预期的那样工作。 It seems that even though the painter has been rotated, it is unaware of the "new" bounds it can paint within, thereby clipping to the "Portrait" size.
看来即使画家已旋转,它也没有意识到它可以在其中绘画的“新”边界,因此裁剪到“肖像”大小。
I have tried the following to no avail: Turning off clipping ( _painter->setClipping(false);
), setting a new clip rect ( _painter->setClipRect(0, 0, _image.height(), _image.width());
), and adjusting the window and viewport in various ways. 我尝试了以下操作,但无济于事:关闭剪辑(
_painter->setClipping(false);
),设置新的剪辑rect( _painter->setClipRect(0, 0, _image.height(), _image.width());
),并以各种方式调整窗口和视口。
I have looked through the documentation of QPainter and QImage, and scoured the internet, but I haven't found this particular issue discussed before. 我浏览了QPainter和QImage的文档,并浏览了Internet,但是之前没有发现这个特殊问题。
As it turns out, the problem was unrelated to my posted code. 事实证明,问题与我发布的代码无关。 Here is my solution, in case anyone runs into this issue in the future.
这是我的解决方案,以防将来有人遇到此问题。
The problem originally came about during implementation of a custom QPrintEngine/QPaintEngine class. 该问题最初是在自定义QPrintEngine / QPaintEngine类的实现期间发生的。 The code posted in the question works-- however , I had forgotten to update the
QPrintEngine::property()
function to return the new dimensions corresponding to the PPK_PageRect
and PPK_PaperRect
keys, when the orientation was set to Landscape. 问题中发布的代码有效- 但是 ,当方向设置为“横向”时,我忘记更新
QPrintEngine::property()
函数以返回与PPK_PageRect
和PPK_PaperRect
键对应的新尺寸。
Note that the QPrintEngine::metric()
function does not appear to need to be updated in this way (in my project). 需要注意的是
QPrintEngine::metric()
函数似乎并不需要以这种方式进行更新(在我的项目)。 I'm assuming this is because the metric
function is mostly used when the QPrinter that utilizes this QPrintEngine implementation is used as a paint device, and that never happens in my project. 我假设这是因为在使用此QPrintEngine实现的QPrinter用作绘画设备时,主要使用
metric
函数,而在我的项目中从来没有发生过。
In any case, fixing this issue allows the QImage to be properly painted on "sideways". 在任何情况下,解决此问题都可以将QImage正确地绘制在“侧面”上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.