QGraphicsItem returns impossible pos() value

99 Views Asked by At

Qt 4.8 Linux

When move QGraphicsRectItem with mouse and item visualy remains inside the scene:

auto new_bnd_rect = child->sceneBoundingRect();
qDebug()<<new_bnd_rect.topLeft()<<child->scenePos()<<child->pos();
qDebug()<<new_bnd_rect;
qDebug()<<dynamic_cast<QGraphicsRectItem*>(child)->rect()<<child->scene()->sceneRect()<<child->scene()->views()[0]->sceneRect();

Result:

debug: QPointF(-1.5,-1.5) QPointF(-142.597,-208.512) QPointF(-142.597,-208.512)
debug: QRectF(-1.5,-1.5 -139.097x-205.012)
debug: QRectF(142.597,208.512 -142.097x-208.012) QRectF(0,0 711x922) QRectF(0,0 711x922)

There are some impossible values:

  1. Scene TopLeft = {0,0}, but rectangle have negative position
  2. Negative result of QGraphicsRectItem()::rect()::height()
  3. result of QGraphicsItem::sceneBoundingRect()::topLeft() != result of QGraphicsItem::scenePos()
  4. sceneBoundingRect() returns negative values

Minimal Example

class ControlledRect : public QObject, public QGraphicsRectItem
{
    Q_OBJECT
public:
    QVariant itemChange(GraphicsItemChange change, const QVariant &value);
signals:
    void moved_to(QPointF);
};

QVariant ControlledRect::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
{
    if (change == ItemPositionChange && scene()) {
        // value is the new position.
        QPointF newPos = value.toPointF();
        emit moved_to(newPos);
    }
    return QGraphicsItem::itemChange(change, value);
}

class Control : public QObject
{
    Q_OBJECT
ControlledRect* child;
public slots:
void send_rect_changed(QPointF new_pos);
};

void Control::send_rect_changed(QPointF new_pos)
{
    auto new_bnd_rect = child->sceneBoundingRect();
    qDebug()<<new_bnd_rect.topLeft()<<child->scenePos()<<child->pos();
    qDebug()<<new_bnd_rect;
    qDebug()<<dynamic_cast<QGraphicsRectItem*>(child)->rect()<<child->scene()->sceneRect()<<child->scene()->views()[0]->sceneRect();
}

Control::Control()
{
...
connect(child, SIGNAL(moved_to(QPointF)), this, SLOT(send_rect_changed(QPointF)), Qt::QueuedConnection);
}

0

There are 0 best solutions below