简体   繁体   English

使用旋转的QPainter绘画将剪辑裁剪到QImage的错误区域

[英]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_PageRectPPK_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.

相关问题 当QDeclarativeItem上的X,Y不为0时,QPainter :: drawImage()剪辑QImage - QPainter::drawImage() clips QImage when X, Y are not 0 on a QDeclarativeItem 使用QPainter确定在绘制时QImage的哪些像素被更改 - Determine which pixels of QImage were changed while painting on it with QPainter 如何在QScrollArea上绘制QImage? 做到了这一点,但有一些小问题QPainter :: begin:小部件绘画只能作为paintEvent的结果开始 - How to draw QImage on QScrollArea? Did this, but have some minor problems QPainter::begin: Widget painting can only begin as a result of a paintEvent 在QPainter :: end()之前调用QImage :: save()是否安全? - Is it safe to call QImage::save() before QPainter::end() QPainter 仅在 QImage 上绘制矩形的一部分 - QPainter paints only part of the rectangle over QImage qpainter绘画替代品(性能在Mac上很糟糕) - qpainter painting alternatives (performance sucks on Mac) Qt中的QPaintEvent绘制区域? - QPaintEvent painting region in Qt? Qt-QPainter没有绘制带有纵横比的QPixmap - Qt - QPainter not painting QPixmap w/ Aspect Ratio 使用一个QPainter一次绘制多个输出:SVG和QImage - Use one QPainter to paint multiple outputs at once: SVG and QImage QPainter :: begin:小部件绘画只能作为paintEvent的结果开始 - QPainter::begin: Widget painting can only begin as a result of a paintEvent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM