如果我将项目设置为可选,QGraphicsPixmapItem是一个太大的像素(QGraphicsPixmapItem is one pixel too large if I set the item

编程入门 行业动态 更新时间:2024-10-21 17:43:45
如果我将项目设置为可选,QGraphicsPixmapItem是一个太大的像素(QGraphicsPixmapItem is one pixel too large if I set the item selectable)

我的QGraphicsPixmapItem必须报告正确的大小 - 其初始大小应与原始图像大小匹配。

如果我让项目可选,我会注意到一些奇怪的事情:报告的大小是一个像素太大。

这是预期的吗? 对于所有可设置的QGraphicsPixmapItems ,此行为是否一致? (因此,我是否可以覆盖boundingRect()以从每次QGraphicsPixmapItem::boundingRect()报告的大小中减去1?)

简单检查,任何图像:

QGraphicsPixmapItem p; p.setFlags(QGraphicsItem::ItemIsSelectable); QString fileName = QFileDialog::getOpenFileName(0, QObject::tr("Open Image File"), QString(), QObject::tr( "Png files (*.png);;Jpeg files (*.jpg *.jpeg);;Bitmap files (*.bmp)")); QPixmap pixmap(fileName); qDebug("%d %d", pixmap.size().width(), pixmap.size().height()); p.setPixmap(pixmap); qDebug("%f %f", p.boundingRect().width(), p.boundingRect().height());

My QGraphicsPixmapItem has to report a correct size - its initial size should match the original image size.

I notice something odd if I make the item selectable: the size reported is one pixel too large.

Is this to be expected ? Will this behavior be consistent for all QGraphicsPixmapItems that are set selectable ? (And can I therefore override the boundingRect() to subtract 1 from the size reported by the QGraphicsPixmapItem::boundingRect() each time ?)

Simple check, with any image:

QGraphicsPixmapItem p; p.setFlags(QGraphicsItem::ItemIsSelectable); QString fileName = QFileDialog::getOpenFileName(0, QObject::tr("Open Image File"), QString(), QObject::tr( "Png files (*.png);;Jpeg files (*.jpg *.jpeg);;Bitmap files (*.bmp)")); QPixmap pixmap(fileName); qDebug("%d %d", pixmap.size().width(), pixmap.size().height()); p.setPixmap(pixmap); qDebug("%f %f", p.boundingRect().width(), p.boundingRect().height());

最满意答案

这是预期的行为。 如果查看源代码 ,您会看到在设置ItemIsSelectable标志时它向每个方向添加了半个像素:

if (d->flags & ItemIsSelectable) { qreal pw = 1.0; return QRectF(d->offset, d->pixmap.size()).adjusted(-pw/2, -pw/2, pw/2, pw/2); }

This is expected behavior. If you look at the source code, you will see that it adds half a pixel to each direction when the ItemIsSelectable flag has been set:

if (d->flags & ItemIsSelectable) { qreal pw = 1.0; return QRectF(d->offset, d->pixmap.size()).adjusted(-pw/2, -pw/2, pw/2, pw/2); }

更多推荐

本文发布于:2023-07-24 10:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1244989.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:是一个   太大   我将   可选   设置为

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!