I have just figured it out.
My first misunderstanding was that the boundingRect()'s coordinates must be (-0.5,-0.5) which means they are relative to the current item's position and not absolute on the Scene. and so this is the correct one:
QRectF GIPoint::boundingRect() const {
// return QRectF(pos().x(), pos().y(), 5, 5);
return QRectF(-0.5, -0.5, 5, 5);
}
My second misunderstanding was in paint(). Do not use the pos() or scenePos() coordinates in order to draw the item. Instead draw it at (0,0). So the corrected paint() is this:
void GIPoint::paint(
QPainter *painter,
const QStyleOptionGraphicsItem *option,
QWidget *widget
){
(void )option;
(void )widget;
QRectF rect = boundingRect();
qWarning() << "painting: scenePos " << scenePos() << ", rect " << rect;
QBrush brush = QBrush(Qt::black, Qt::SolidPattern);
painter->fillRect(rect, brush);
//painter->drawRect(rect);
}
The original program in my question still needs to be modified. Or shall I edit the changes in?