如果我将项目设置为可选,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());

这是预期的行为。如果您查看 source code,您会看到当设置 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);
}