我的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); }更多推荐
发布评论