QGraphicsItem 返回不可能的 pos() 值

QGraphicsItem returns impossible pos() value

提问人:zbuffer 提问时间:11/25/2020 最后编辑:zbuffer 更新时间:11/25/2020 访问量:89

问:

Qt 4.8 Linux操作系统

当使用鼠标移动QGraphicsRectItem时,项目视觉效果仍保留在场景中:

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();

结果:

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)

有一些不可能的值:

  1. 场景 TopLeft = {0,0},但矩形具有负位置
  2. QGraphicsRectItem()::rect()::height() 的阴性结果
  3. QGraphicsItem::sceneBoundingRect()::topLeft() 的结果 != QGraphicsItem::scenePos() 的结果
  4. sceneBoundingRect() 返回负值

最小示例

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);
}

qt qgraphicsscene qt4.8

评论

1赞 eyllanesc 11/25/2020
请提供一个最小的可重复示例
0赞 G.M. 11/25/2020
我看不出这里有什么“不可能”的。根据您正在执行的操作,场景矩形可能会自动调整以确保保持可见。QGraphicsRectItem
0赞 zbuffer 11/25/2020
如何拥有物品!=物品的位置?child->sceneBoundingRect().topLeft();返回 QPointF(-1.5,-1.5),同时 child->scenePos() 返回 QPointF(-142.597,-208.512)

答: 暂无答案