提问人:zbuffer 提问时间:11/25/2020 最后编辑:zbuffer 更新时间:11/25/2020 访问量:89
QGraphicsItem 返回不可能的 pos() 值
QGraphicsItem returns impossible pos() value
问:
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)
有一些不可能的值:
- 场景 TopLeft = {0,0},但矩形具有负位置
- QGraphicsRectItem()::rect()::height() 的阴性结果
- QGraphicsItem::sceneBoundingRect()::topLeft() 的结果 != QGraphicsItem::scenePos() 的结果
- 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);
}
答: 暂无答案
评论
QGraphicsRectItem